Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

read a file twice...

by gdnew (Acolyte)
on Apr 15, 2002 at 03:53 UTC ( #159085=perlquestion: print w/replies, xml ) Need Help??

gdnew has asked for the wisdom of the Perl Monks concerning the following question:

Is there anyway to read a file twice without having to close it first and open it again?
For eg: the first write is only to find the max and minimum.
The next one is to print some part of the file preceeded by the max and min.

Thank you in advanced.

Replies are listed 'Best First'.
Re: read a file twice...
by rob_au (Abbot) on Apr 15, 2002 at 04:07 UTC
    In addition to the approach involving reading the file into an array as suggested by Chmrr above, I would introduce you to the perlfunc:seek function, which allows you to move the current file read/write pointer within a file. With this command, you can move back to the start of the file and re-read it in full without closing and reopening the file. Eg.

    open F, 'filename.ext' or die $!; . . seek F, 0, 0; # Move pointer back to start of file

    This approach is advantageous where the file may be particularly large in size and resources may be constrained.


Re: read a file twice...
by Chmrr (Vicar) on Apr 15, 2002 at 03:58 UTC

    You can "read" it as many times as you want if you stuff it into an array just once:

    open F, "some_file" or die $!"; my @lines = <F>; close F; for (@lines) { # .. find the max and minimum } for (@lines) { # .. do something before / after those indexes }

    Update: Oops. s/\@f/\@lines/g; as MeowChow pointed out to me.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: read a file twice...
by CukiMnstr (Deacon) on Apr 15, 2002 at 06:24 UTC
    besides the two approaches given above, you could give Tie::File by Dominus a try. It would allow you to manipulate a text file as a perl array (each file record, line per default, corresponding to an element of the array) without loading the whole file into memory, so it is specially useful for large files. Also useful when modifying files...

    hope this helps,

Re: read a file twice...
by thor (Priest) on Apr 15, 2002 at 12:26 UTC
    In the spirit of TMTOWTDI, why not just keep track of the text along the way? If the number and the text are on the same line, easy money. Here's what I've got (I'm going to assume the format <text>:<number>)
    while(<FILE>) { @array = split ':'; if (@array[1] > $max) #number we read in is greater than current max { $max = $array[1]; $max_text = $array[0]; } #similarly for min }
    Of course, your file layout may be more complex.
Re: read a file twice...
by clintp (Curate) on Apr 15, 2002 at 15:56 UTC
    In the spirit of TIMTOWDI and filed under "don't do this at home":
    #!/usr/bin/perl -w push(@ARGV, "filetoread", "filetoread"); while(<>) { print; } continue { # This block not necessary, but for fun only print "Second verse, same as the first!\n" if (eof ARGV && @ARGV); }
Re: read a file twice...
by gdnew (Acolyte) on Apr 15, 2002 at 06:50 UTC
    Hi.. thanks everyone...
Re: read a file twice...
by dsb (Chaplain) on Apr 15, 2002 at 16:37 UTC
    $ perldoc -f seek

    For what it's worth, be sure to read how to use whence properly. I misread that part and I misused 'seek' for awhile.

    Amel - f.k.a. - kel

Re: read a file twice...
by wardk (Deacon) on Apr 15, 2002 at 17:36 UTC
    This sort of issue comes up alot in my shop with generating log files, typically for a scheduled "batch" job. the logfile for usability purposes needs the totals, stats, etc at the top of the log with any detail info after.

    To avoid processing twice (it's usually processing a SQL cursor, but occasionally a flat file too), we typically use the method mentioned by thor.... read the file once, saving the lines to be printed afterward in one or more arrays, then pushing them out at the end.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://159085]
Approved by rob_au
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2020-04-07 10:55 GMT
Find Nodes?
    Voting Booth?
    The most amusing oxymoron is:

    Results (42 votes). Check out past polls.