CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Working with text files

 



randolf00
Novice


Feb 19, 2004, 6:23 PM

Post #1 of 5 (598 views)
Working with text files Can't Post

Hi there. Im new to using perl and have a question about working with text files. I have written a script that saves data to a text file in this format:

blahID
blah
blah
blah
blah
1
__NexTDETAILS__

When i add more details it is added exacly the same on the following line and I use the __NexTDETAILS__ to split the file so i can distinguish between the different details.

Is there a way that I could search the text file for the relevant "blahID" and increment the number that is five lines below it?? Im not sure if this is even possible or if its the best way to go about it but any help or advice would be appreciated.

Thanks. Crazy
************Starting to understand**********


davorg
Thaumaturge / Moderator

Feb 20, 2004, 12:15 AM

Post #2 of 5 (593 views)
Re: [randolf00] Working with text files [In reply to] Can't Post

Well, you could do it manually with code something like this:


Code
while (<FILE>) { 
if (/blahID/) {
# ignore next 4 lines
<FILE> for 1 .. 4;
my $id = <FILE>;
}
}


But personally I think I'd use $/ to process the file a line at a time.


Code
$/ = "__NexTDETAILS__\n"; 

while (<FILE>) {
# whole record is in $_
chomp; # removes $/
my $id = /(\d+)$/; # get the last set of digits in the record
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


randolf00
Novice


Feb 20, 2004, 1:35 PM

Post #3 of 5 (587 views)
Re: [davorg] Working with text files [In reply to] Can't Post

Thanks for the reply but Im a little confused... Unsure

Im using the following code to open the file...would the code given go directly into this??

sysopen(FH, "list.txt", O_RDWR|O_APPEND|O_CREAT) or die "can't open $filename: $!";
flock(FH, LOCK_EX) or die "can't lock $filename: $!";

close(FH);

How would i write the increment the number back into the file at the correct place??

Thanks for your advice so far. Im away to play around with what you suggested and see what I can do....Wink


PS Im very new to perl
************Starting to understand**********

(This post was edited by randolf00 on Feb 20, 2004, 1:37 PM)


davorg
Thaumaturge / Moderator

Feb 23, 2004, 1:30 AM

Post #4 of 5 (577 views)
Re: [randolf00] Working with text files [In reply to] Can't Post

Sorry, I didn't see the bit about incrementing the number in place.

Incrementing data in place whilst processing a file is a rather hard job. It gets tricky when the data item you are updating changes size. It's probably easier to update the data in a _new_ copy of the file and then to copy the new file over the old one when you have finished.

Another alternative would be to look at the Tie::File module that is a standard part of the current Perl distribution.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


randolf00
Novice


Feb 23, 2004, 3:15 AM

Post #5 of 5 (575 views)
Re: [davorg] Working with text files [In reply to] Can't Post

Thanks..I will look into that. Cool
************Starting to understand**********

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives