in reply to for vs foreach

I'm the complete opposite. I can't remember the last time that I used for. It always seems to me that for iteraring over a list, the foreach syntax is more natural. I find

foreach (@array) { # do someting with $_ }

far easier to follow than

for ($i = 0; $i < @array; ++$i) { # do something with $array[$i] }

If, for some reason you need the array index for some calculation, then you can use

foreach $i (0 .. $#array) { # do something with $array[$i] }

Of course, if you're really keen on saving keystrokes you can always spell foreach as for.

--
<http://www.dave.org.uk>

European Perl Conference - Sept 22/24 2000, ICA, London
<http://www.yapc.org/Europe/>

Replies are listed 'Best First'.
RE: Re: for vs foreach
by vkonovalov (Monk) on Jul 14, 2000 at 14:08 UTC
    for me
    for (@array) { # do someting with $_ }
    is quite natural, and
    for my $i (0..$#array) {...
    is used quite often by me...

    I'm still searching for perl wisdom to find different behaviour between for and foreach