Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^5: Counting keys in a hash

by Athanasius (Chancellor)
on Nov 20, 2012 at 14:50 UTC ( #1004729=note: print w/replies, xml ) Need Help??

in reply to Re^4: Counting keys in a hash
in thread Counting keys in a hash

The loop will still work if the data is in an array, won't it?

No, the diamond operator <> is special. In the original loop:

while (<DATA>) {

one line is read on each iteration, and the loop continues until <DATA> returns undef to signal that the data accessed via the filehandle (DATA in this case) has all been read in.

But if you have your data in an array (presumably one line per element), you need a for loop (also spelled foreach — they’re the same):

for (@log) { <code here> }

which iterates over the array until each element has been processed.

(Note that in both loops, the line/element read in on a single iteration is stored in the special variable $_.)

Hope that helps,

Athanasius <°(((><contra mundum

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004729]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-06-24 06:20 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.