The Ghost wrote:

sub getIndex {
    my ($array, $value)[EMAIL PROTECTED];
    my $x=0;
foreach (@{$array}) { $_ eq $value ? return $x : $x++; } #only works if array has unique items and is slow, but could be easy if it fits your problem #personally, I don't recommend this, but maybe it's what you were thinking of?
    }

#!usr/bin/perl
my @arr = qw/a b c d e a/; #last element will list as zero!
for(@arr)
{
    my $num=getIndex([EMAIL PROTECTED], $_);
    print "The No.$num element is $_\n";
}

Oi, don't do that :)

You exponentially (sort of) loop over the list by looping the list again for each item in the list.


This is efficient and fast (and easy to read and maintain)
*and* it will work with things like last(), redo(), etc...:

for my $idx (0 .. $#arr) {
    print "Item number $idx is $arr[$idx]\n";
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to