For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2006 > the way to get current index of an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author the way to get current index of an array
Zhao Bingfeng

2006-02-28, 3:56 am

John W. Krahn

2006-02-28, 3:56 am

zhao_bingfeng@topsec.com.cn wrote:
> hi, perlers,


Hello,

> 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";
> }


for ( 0 .. $#arr )
{
print "The No. $_ element is $arr[$_]\n";
}



John
--
use Perl;
program
fulfillment
JupiterHost.Net

2006-02-28, 6:56 pm



zhao_bingfeng@topsec.com.cn wrote:
> hi, perlers,


Howdy,

> Is there a simple way to get the current index of an array in loop


yes

> 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";
> }


for my $idx (0 .. $#arr) {
print "The no $idx element is $arr{$_}\n";
}

my $idx = 0;
for my $item(@arr) {
print "The no $idx element is $item\n";
$idx++;
}

There are other more magical ways but those will probably be best for you :)

The first way not only has less code but is faster.

HTH :)
Bob Showalter

2006-02-28, 6:56 pm

zhao_bingfeng@topsec.com.cn 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";
> }


There is no built-in method for determining the array index, because
for() and foreach() can iterate over things other than arrays.

So you have to keep track of the index yourself, either by iterating
over indexes like John showed, or keeping a separate variable:

my $i = 0;
for (@arr) {
print "The current element $_ is index $i\n";
}
continue {
$i++;
}

The Ghost

2006-02-28, 6:57 pm

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@topsec.com.cn>
<zhao_bingfeng@topsec.com.cn> 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.
>
>



JupiterHost.Net

2006-02-28, 6:57 pm



The Ghost wrote:

> 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";
> }


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";
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com