in reply to Finding index of an entry in an Array

Assuming you're wanting a simple iteration and not some type of sort algorithm, this will do the trick.
my $index = 0; my $input = $ARGV[0] || 'bar'; my @repo = qw (foo bar qux); $index++ until @repo[$index] eq $input or $index > $#repo; #don't want + to loop forever if it's not found
It's worth noting that in your example, bar is at index 1, not index 2.

Update: Fixed the typo pointed out by dwu

Replies are listed 'Best First'.
Re^2: Finding index of an entry in an Array
by dwu (Monk) on Dec 07, 2007 at 02:57 UTC
    $index++ until @repo[$index] eq $input or $i > $#repo;
    Did you perhaps mean:
    $index++ until $repo[$index] eq $input or $index > $#repo;

    since you don't declare $i anywhere, and I'd have a hard time not getting that to loop forever when I started comparing zero to $#repo :)

    Edit: thanks to IRCperson for spotting $repo[$index]