Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

File handle question

by anniyan (Monk)
on Aug 12, 2005 at 06:43 UTC ( #483193=perlquestion: print w/replies, xml ) Need Help??
anniyan has asked for the wisdom of the Perl Monks concerning the following question:


I have a very silly and simple question, but i dont understand why it works like this.

First while loop working properly whereas the second is not, why?

undef $/; open (FCMP, "anchor.txt") or die ("anchor.txt is not opened\n"); { local $/="\n"; while(<FCMP>) { print "$_\n"; chomp; @item = split(/\t/); $pii{$item[0]} = [$item[1], $item[2], $item[3]]; } } { local $/="\n"; while (<FCMP>) { my $an = $_; print "$an\n"; } }

The second while statement is not printing anything. So i thought that only one time a file handle can be used directly <FCMP> without assigning to a variable. Is it so? If wrong where am i going wrong.


Replies are listed 'Best First'.
Re: File handle question
by Corion (Pope) on Aug 12, 2005 at 06:48 UTC

    Once you step through a file, you need to rewind it (if possible) to read through it again. Your first loop steps through the file to the end, so the second loop doesn't have any lines to step over.

    You can either reopen FCMP, or rewind it to position 0:

    # open/reopen my $filename = "anchor.txt"; open FCMP, "<", $filename or die "Couldn't open '$filename': $!"; # or, alternatively seek FCMP,0,0; # rewind
Re: File handle question
by Zaxo (Archbishop) on Aug 12, 2005 at 06:47 UTC

    You need to seek back to the beginning of the file. The first loop exhausted it, so the second begins at eof().

    After Compline,

Re: File handle question
by hatter (Pilgrim) on Aug 12, 2005 at 12:15 UTC
    If you start off with
    open (FCMP, "anchor.txt") or die ("anchor.txt is not opened\n"); my @file = <FCMP>;
    you can then replace while(<FCMP>) with foreach (@file) and the replace the second loop in its entirety, with print join("\n",@file)

    If you want bonus cleverness, you could even replace the first loop with a single map() command, but that's probably leaping a step or two up on the learning curve.

    Either way, as others have said, using <> steps through each line of input, you can either seek or re-open the file to go back to the beginning, or simply read the file into an array first, which avoids having to read the file from disk an extra time (a more expensive operation)

    the hatter
Re: File handle question
by murugu (Curate) on Aug 12, 2005 at 09:23 UTC


    In the first loop itself eof(end of the file) is reached. So in second loop nothing is getting printed.

    Murugesan Kandasamy.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://483193]
Approved by friedo
[Discipulus]: another tybalt's masterpiece!
[Discipulus]: tybalt89 you must consider writing a book: The Perl regex approach to every problem

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-11-23 08:11 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (331 votes). Check out past polls.