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. :)