Re: the way to get current index of an array
- From: ghost@xxxxxxxxxxxxx (The Ghost)
- Date: Tue, 28 Feb 2006 08:56:10 -0600
sub getIndex {
my ($array, $value)=@_;
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(\@arr, $_);
print "The No.$num element is $_\n";
}
On Feb 27, 2006, at 11:07 PM, <zhao_bingfeng@xxxxxxxxxxxxx> <zhao_bingfeng@xxxxxxxxxxxxx> wrote:
hi, perlers,
Is there a simple way to get the current index of an array in loop statement? the procondition is you cannot get this information from array element. For example
#!usr/bin/perl
my @arr = qw/a b c d e/;
for(@arr)
{
print "The No.?? element is $_\n";
}
-----------------------------------------------
Life is a different teacher...
It doesn't teach lessons, and then keep exams...
It keeps the exams first and then teaches the lessons.
- Follow-Ups:
- Re: the way to get current index of an array
- From: JupiterHost.Net
- Re: the way to get current index of an array
- References:
- the way to get current index of an array
- From: Zhao Bingfeng
- the way to get current index of an array
- Prev by Date: Re: Is there a GNU-like "mkdir -p" method in Perl?
- Next by Date: Re: Is there a GNU-like "mkdir -p" method in Perl?
- Previous by thread: Re: the way to get current index of an array
- Next by thread: Re: the way to get current index of an array
- Index(es):
Relevant Pages
|
|