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

Re: Tidying up some dereferencing code

by uwevoelker (Pilgrim)
on Aug 13, 2002 at 14:15 UTC ( #189797=note: print w/replies, xml ) Need Help??


in reply to Tidying up some dereferencing code

foreach (@{$self->{_words}}) { foreach my $curr (@{$_->pass_matches}) { # ... } }
Better use foreach instead of for, when you iterate over an array.

Replies are listed 'Best First'.
(jeffa) 2Re: Tidying up some dereferencing code
by jeffa (Bishop) on Aug 13, 2002 at 16:32 UTC

    "Better use foreach instead of for, when you iterate over an array."

    Why, they do the exact same thing:
    print $_ for qw(foo bar baz); print $_ foreach qw(foo bar baz); $ perl -MO=Deparse foo.pl foreach $_ ('foo', 'bar', 'baz') { print $_; } foreach $_ ('foo', 'bar', 'baz') { print $_; } foo.pl syntax OK
    I will admit that i tend to use foreach when i iterate over a 'collection', but never hardly ever (never say never...) when iterating over arrays. As a matter of fact, i have really started to favor for over foreach because foreach is just too wordy. But, consitency is alway king. :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2022-05-21 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?