in reply to Finding the index of a specific element in an array.

for (my $i = 0; $i < @array; $i++) { if ($array[$i] == 7) { say "The index is $i"; last; } }
Hey! A "C-style" for loop. I guess it contains an off-by-one error.

Replies are listed 'Best First'.
Re^2: Finding the index of a specific element in an array.
by chromatic (Archbishop) on Jan 24, 2012 at 23:24 UTC
    I guess it contains an off-by-one error.

    No, but it does contain synthetic code.

      No, but it does contain synthetic code.
      Yeah, and? Care to show us some 'natural' code which actually describes and solves the problem in hand and that doesn't have any an artifact of the particular method that you have chosen to solve the problem.? The emphasis are verbatim quotes from the article you link to.
        for my $i (0 .. $#array) { next unless $array[$i] == 7; say "The index is $i"; }

        You may quibble that the use of $#array is itself an artifact of the method I've chosen to solve the problem (and I can agree), but there's less synthetic code in this version. Certainly there's iteration and incrementing, but when Perl handles that and the programmer doesn't have to, there's less synthetic code.