Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with file appending 1

Status
Not open for further replies.

martinjburgess

IS-IT--Management
Jun 8, 2012
21
GB
Hi I have a little routine that I am struggling with.

Bacically, I need to open a file and read the contents.
The contents are one entry per line in this form: 1339142221
The file name is $username.term
The other variable I have is $messid

What I need to do is read the file and compare the entries against $messid.
If $messid is not matched in the file then append the file with the new $messid.

Here is one of my attempts:

Code:
	fopen(FILE,"+>$members/$username.term");
	foreach(@list) {
			($msgid) = split(/\|/,$_);

	if($_ eq $msgid) { next; }

	else {

	print "$messid\n"; }
	}

Any help appreciated.
 
Hi

Please pick an Indent style and use it consistently. Makes your code easier to read and debug.

Your code needs some explanations :
[ul]
[li]Where are you reading from the file ? Into which variable ?[/li]
[li]What is the content of the @list variable ? Why are you [tt]split()[/tt]ing its values ?[/li]
[li]What are you trying to accomplish with that [tt]next[/tt] call ?[/li]
[li]Where should the writing to the file happen ?[/li]
[/ul]

Feherke.
[link feherke.github.com/][/url]
 
Thanks for the reply I am a novice at perl programming that is why I am here seeking help, my attempt is more than likely useless.

The file I am reading is called $username.term and has this layout:

1339142221
1339142970
1339143224
etc

I wish to read from the top of the file and see if any of the entries match an existing variable called $messid. If there is no match then append the file at the bottom of the list with $messid.

Example 1.
Lets say the $messid = 1339142970
The file is read and it matches the contents of one of the lines so does nothing.

Example 2.
Lets say that $messid = 1339144266
The file is read and no match is found so the 1339144266 ($messid) is appended to the bottom of the list.

I apologise for my lack of perl knowledge.

Many Thanks





I
 
Hi

Some things to note :
[ul]
[li]There is no fopen in Perl. Just [tt]open[/tt].[/li]
[li]The [tt]+>[/tt] access mode clobbers the file on read.[/li]
[/ul]
Perl:
[navy]$messid[/navy][teal]=[/teal][green][i]'1339144266'[/i][/green][teal];[/teal]

[b]open[/b] FILE[teal],[/teal][green][i]"+<$members/$username.term"[/i][/green][teal];[/teal]

[navy]$found[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal]
[b]while[/b] [teal]([/teal][b]chomp[/b][teal]([/teal][navy]$line[/navy][teal]=[/teal][green][i]<FILE>[/i][/green][teal]))[/teal] [teal]{[/teal]
  [navy]$found[/navy][teal]=[/teal][purple]1[/purple] [b]if[/b] [navy]$line[/navy] [b]eq[/b] [navy]$messid[/navy][teal];[/teal]
[teal]}[/teal]

[b]print[/b] FILE [green][i]"$messid\n"[/i][/green] [b]unless[/b] [navy]$found[/navy][teal];[/teal]

[b]close[/b] FILE[teal];[/teal]

Feherke.
[link feherke.github.com/][/url]
 
Thank you for being so prompt.

Can I explain that $messid is a message id and changes with each message. The variable must be similar on this forum it is probably $qid, in the address bar it says qid=1685298 for this post.

The file $username.temp is not being created when a member accesses the post for the first time so there is nothing to read.

Is there a routine that will create the empty file first if it doesn't exist?

Cheers
 
Hi

Note that the above code supposes that you may want to do other operations too.

If the only reason of the file reading & writing is the search and appending of $messid, you can optimize it : as soon as you find the matching code, jump directly to the end of file.
Perl:
[navy]$messid[/navy][teal]=[/teal][green][i]'1339144266'[/i][/green][teal];[/teal]

[b]open[/b] FILE[teal],[/teal][green][i]"+<$members/$username.term"[/i][/green][teal];[/teal]

[navy]$found[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal]
[b]while[/b] [teal]([/teal][b]chomp[/b][teal]([/teal][navy]$line[/navy][teal]=[/teal][green][i]<FILE>[/i][/green][teal]))[/teal] [teal]{[/teal]
  [b]if[/b] [teal]([/teal][navy]$line[/navy] [b]eq[/b] [navy]$messid[/navy][teal])[/teal] [teal]{[/teal]
    [navy]$found[/navy][teal]=[/teal][purple]1[/purple][teal];[/teal]
    [b]seek[/b] FILE[teal],[/teal][purple]0[/purple][teal],[/teal][purple]2[/purple][teal];[/teal]
  [teal]}[/teal]
[teal]}[/teal]

[b]print[/b] FILE [green][i]"$messid\n"[/i][/green] [b]unless[/b] [navy]$found[/navy][teal];[/teal]

[b]close[/b] FILE[teal];[/teal]

This can be simplified even more by not using a separate variable to store the found status, but simply closing the file, then checking its status :
Perl:
[navy]$messid[/navy][teal]=[/teal][green][i]'1339144266'[/i][/green][teal];[/teal]

[b]open[/b] FILE[teal],[/teal][green][i]"+<$members/$username.term"[/i][/green][teal];[/teal]

[b]while[/b] [teal]([/teal][b]chomp[/b][teal]([/teal][navy]$line[/navy][teal]=[/teal][green][i]<FILE>[/i][/green][teal]))[/teal] [teal]{[/teal]
  [b]close[/b] FILE [b]if[/b] [navy]$line[/navy] [b]eq[/b] [navy]$messid[/navy][teal];[/teal]
[teal]}[/teal]

[b]if[/b] [teal]([/teal][b]tell[/b][teal]([/teal]FILE[teal])!=-[/teal][purple]1[/purple][teal])[/teal] [teal]{[/teal]
  [b]print[/b] FILE [green][i]"$messid\n"[/i][/green][teal];[/teal]

  [b]close[/b] FILE[teal];[/teal]
[teal]}[/teal]


Feherke.
[link feherke.github.com/][/url]
 
Hi

martinjburgess said:
Is there a routine that will create the empty file first if it doesn't exist?
Well, [tt]open[/tt] will create it when used with mode [tt]>[/tt], [tt]>>[/tt] or [tt]+>[/tt].

So you will have to check the existence of the file and use either [tt]+<[/tt] or [tt]+>[/tt] mode accordingly :
Code:
[b]open[/b] FILE[teal],-[/teal]f [green][i]"$members/$username.term"[/i][/green][teal]?[/teal][green][i]'+<'[/i][/green][teal]:[/teal][green][i]'+>'[/i][/green][teal],[/teal][green][i]"$members/$username.term"[/i][/green][teal];[/teal]

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top