Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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]
[GotToBTru]: I appreciate the ability to highlight matching brackets/ parentheses/ braces, both for my own code and the inconsistently indented code of others
[Corion]: I like editors that automatically highlight the matching parenthesis (like % in vi), but that's roughly the extent to which I like editor support ;)
[GotToBTru]: same here Corion.
[Corion]: I tried for a short time (well, 8 hours now) to get Perl::Tidy set up but then found that it doesn't support (new-style) signatures and then stopped again ;))
[GotToBTru]: changing the font color of the rest of the document just because I typed a single quote character is annoying

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-02-27 12:51 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (385 votes). Check out past polls.