Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Cant use array with numbers??

by Corion (Patriarch)
on Sep 18, 2007 at 08:21 UTC ( [id://639578]=note: print w/replies, xml ) Need Help??


in reply to Cant use array with numbers??

Read for.

Normally, you want to provide C++ and Perl with syntactically correct code though.

The way of performing a loop over a list of array elements in Perl is usually:

foreach (@Array) { next if $_ eq "DontPrintMe"; print $_; };

Replies are listed 'Best First'.
Re^2: Cant use array with numbers??
by robot_tourist (Hermit) on Sep 18, 2007 at 11:00 UTC

    I like to put an explicit name in instead of $_, like so:

    for my $value (@array) { do_something_with($value); }

    Going back to the C/C++ style comparison, I also think $value is more readable than $array[$i], but that's a matter of opinion and/or of naming things well.

    But remember that $value is not available outside the for loop. If you're worried about knowing where you are in the array, I've found I don't need to worry most of the time or I can just create put 'my $i = 0;' before the loop and do $i++ each time round.

    How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
    Robot Tourist, by Ten Benson

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-28 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found