Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: Counting keys in a hash

by Athanasius (Monsignor)
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


Comment on Re^5: Counting keys in a hash
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2014-07-30 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls