in reply to Re: Accessing array elements
in thread Accessing array elements
I do use the C style for loop if I want the index, because, IMO, it's easier to avoid off-by-one errors than using foreach style. The latter means you either have to use '- 1', or remember to use '$#'.
is standard idiom which translates easily between languages. Bothfor (my $i = 0; $i < @array; $i++) {...}
andforeach my $i (0 .. @array - 1) {...}
feel artificial, and Perl specific to me. And they both have their ugliness (either -1 or $#array).foreach my $i (0 .. $#array) {...}
|
|---|