Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

File handle question

by anniyan (Monk)
on Aug 12, 2005 at 06:43 UTC ( [id://483193]=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

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.

Regards,
Anniyan
(CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

Replies are listed 'Best First'.
Re: File handle question
by Corion (Patriarch) 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,
    Zaxo

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

    Hi,

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

    Regards,
    Murugesan Kandasamy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://483193]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-24 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found