Magic var for Current "Index" in array within loop?

L

Lord0

Hi there,

Is there a magic variable which will return the current index of an array if
used in a loop?

i.e.

# I don't like
my $i=0;

for(@my_array){
# do something using $i
$i++;
}

# I would like to
for(@my_array){
# do something using "magic" variable
# So in third iteration of loop the magic variable would be 2 etc
}

I know about $[ and $# but they don't do the do...........

Cheers

Lord0

http://ltierney.demon.co.uk
 
T

Tore Aursand

Is there a magic variable which will return the current index of an array if
used in a loop?

No. You have to count it yourself the C-way:

for ( my $i = 0; $i <= $#array; $i++ ) {
print "Element $i = " . $array[$i] . "\n";
}

I don't like that one, either, so I constantly use this one instead:

for my $i ( 0..$#array ) {
print "Element $i = " . $array[$i] . "\n";
}

Eventually:

for ( 0..$#array ) {
print "Element $_ = " . $array[$_] . "\n";
}
 
B

Brian McCauley

Lord0 said:
Is there a magic variable which will return the current index of an array if
used in a loop?

This question has been asked twice in .misc and once in .moderated in
the last week^H^H^H^H fourtnight.

Invisible Array Loop Counter?
Basic newbie questions
On "for (@foo)"

Usually it only pops up once every few weeks.

What the $EXPLETIVE is going on?

Is this just a random statistical variation or has someone set it as homework?

Does anyone want to submit a FAQ entry?

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
L

Lord0

This question has been asked twice in .misc and once in .moderated in
the last week^H^H^H^H fourtnight.

Invisible Array Loop Counter?
Basic newbie questions
On "for (@foo)"

Oops sorry, I must admit I didn't think to look. That won't happen
again..........

As for my reason for asking the question - well rest assured it was nothing
to do with homework. I was just looking at a bit of code and was trying to
refactor it to remove the ugly "counters" etc. I thought that as we had $#
and $[ (knowledge of which I guess would preclude homework) we should
logically have a magic variable for the actual current array index, when
used in a loop. I did RTFM and couldn't find anything - which again
should've been a clue.

I quite like the

for(0..$#foo){
$foo[$_] do blah;
}

solution. Only prob is you have to refer to the array by name which you
don't using the following method:

for(@foo){
# I wish I had a cool magic to use here
$_ do blah
}

Anyway thanks for all the help

Lord0

http://ltierney.demon.co.uk
 
M

Michele Dondi

Is there a magic variable which will return the current index of an array if
used in a loop?

This has been asked exactly a week ago, and IMHO it is a faq, so that
maybe it may be considered for inclusion in the faq. I even remember
having asked the very same question myself once: the answer in any
case is "no"!

It is worth pointing out that on the one hand such a magic variable
would be less useful that one could initially think and that there are
probably "better WTDI", but on the other hand there are still a *few*
cases in which it would actually be mildly useful.


Michele
 
M

Michele Dondi

refactor it to remove the ugly "counters" etc. I thought that as we had $#
and $[ (knowledge of which I guess would preclude homework) we should
logically have a magic variable for the actual current array index, when
^^^^^
^^^^^

The fact is that loops do not have to do *only* with arrays, and in
fact you generally do *not* loop over (lists that actually come from
single) arrays:

for (@a, @b, qw/foo bar baz/) {
$a[$myhypotheticalmagicvar]}=something($_);
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}

Is that what I really want? May well be, but most often it won't!
used in a loop. I did RTFM and couldn't find anything - which again

Again, as I wrote in my other post, IMHO there is a minority of cases
in which the hypothetical magic var would indeed come in handy...


Michele
 
B

Brian McCauley

Lord0 said:
Oops sorry, I must admit I didn't think to look. That won't happen
again..........

I forgot congratuate Lawrence on getting the best subject line of the
4. Good subject lines and search engines are the secret to effective
Usenet. Lawrence may have forgotten to use a search engine himself
but at least he's done his bit to help the next person who does.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
L

Lord0

I forgot congratuate Lawrence on getting the best subject line of the
4. Good subject lines and search engines are the secret to effective
Usenet. Lawrence may have forgotten to use a search engine himself
but at least he's done his bit to help the next person who does.

Cheers for the backhanded compliment
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top