in reply to Re: Determine first element in foreach
in thread Determine first element in foreach

Even when you want to loop over the indexes, you can still use a foreach-style loop:

foreach my $i (0 .. $#mylist) { say "first element" if ( $i == 0 ); }

foreach-style loops are generally a better idea that C-style for loops. They're clearer; more declarative. It's harder to introduce an off-by-one error, and easier to spot a mistake.

Replies are listed 'Best First'.
Re^3: Determine first element in foreach
by AnomalousMonk (Archbishop) on Aug 04, 2014 at 20:45 UTC
    It's harder to introduce an off-by-one error, and easier to spot a mistake.

    Indeed. Consider
        for ($i = 0; $i < $#mylist ; $i++) { ... }
    in reply above:  $i < $#mylist vice  $i <= $#mylist — or did you have that in mind?

      I hadn't even noticed that! (Which is precisely my point.)