Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Line number problem with foreach

by roho (Chancellor)
on Feb 28, 2009 at 21:56 UTC ( #747211=note: print w/replies, xml ) Need Help??


in reply to Re: Line number problem with foreach
in thread Line number problem with foreach

Very interesting. I tried your code, and you are right. $. contains the current line number for each record in the input file. My question is "Why does the presence of semi-colons around the diamond make this work? Do they force scalar mode instead of list mode? Does this mean the file is NOT slurped into a list all at once?

"Its not how hard you work, its how much you get done."

Replies are listed 'Best First'.
Re^3: Line number problem with foreach
by AnomalousMonk (Bishop) on Mar 01, 2009 at 04:30 UTC
    In the C-style for-loop
    foreach (;<$fh>;) { print " $. :$_ \n"; }
    the initialization and finalization clauses of the loop (the parts before the first semicolon and after the second semicolon, respectively) are empty. The only clause specified is the conditional clause (between the two semicolons). When the  <> or readline operator is evaluated in the scalar context of the conditional, it returns a single line at a time until the file is exhausted, when it returns undef.

    This is equivalent to reading  <$fh> in a while-loop.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://747211]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2020-02-17 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (70 votes). Check out past polls.

    Notices?