×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help with file appending

Help with file appending

Help with file appending

(OP)
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.

RE: Help with file appending

Hi

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

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

Feherke.
http://feherke.github.com/

RE: Help with file appending

(OP)
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

RE: Help with file appending

Hi

Some things to note :
  • There is no fopen in Perl. Just open.
  • The +> access mode clobbers the file on read.

CODE --> Perl

$messid='1339144266'; open FILE,"+<$members/$username.term"; $found=0; while (chomp($line=<FILE>)) { $found=1 if $line eq $messid; } print FILE "$messid\n" unless $found; close FILE;

Feherke.
http://feherke.github.com/

RE: Help with file appending

(OP)
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

RE: Help with file appending

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.

CODE --> Perl

$messid='1339144266'; open FILE,"+<$members/$username.term"; $found=0; while (chomp($line=<FILE>)) { if ($line eq $messid) { $found=1; seek FILE,0,2; } } print FILE "$messid\n" unless $found; close FILE;

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 :

CODE --> Perl

$messid='1339144266'; open FILE,"+<$members/$username.term"; while (chomp($line=<FILE>)) { close FILE if $line eq $messid; } if (tell(FILE)!=-1) { print FILE "$messid\n"; close FILE; }

Feherke.
http://feherke.github.com/

RE: Help with file appending

Hi

Quote (martinjburgess)

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

So you will have to check the existence of the file and use either +< or +> mode accordingly :

CODE --> (fragment)

open FILE,-f "$members/$username.term"?'+<':'+>',"$members/$username.term";

Feherke.
http://feherke.github.com/

RE: Help with file appending

(OP)
Thank you so much, all working now.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close