perchance has asked for the wisdom of the Perl Monks concerning the following question:

I tried looking for this, but couldn't find the answer. I wrote something like this: (simplified)
for my $i ( @list ) { print $i; # Now I want to know what number item I'm at }
Does anybody know if Perl retains the index for a current line? Maybe something like the $; variable
for reading from files? I can write it otherwise, I'm just curious.

10x,
perchance

--- Half A World Away

Replies are listed 'Best First'.
Re: Index in For loop
by tachyon (Chancellor) on Jun 27, 2001 at 18:21 UTC

    No special variable is available. Here are some options:

    my @array = (1,2,3,4,5); my $count = 0; for (@array) { print "index: ".$count++." contains $_\n"; } for my $index (0..$#array) { print "index: $index contains $array[$index]\n"; } # if you prefer thinking in terms of lines (like files) for my $line (1..@array) { print "line: $line contains $array[$line-1]\n"; } # reading from a filehandle $count = 0; while (<DATA>) { chomp; next unless $_; $count++; print "line: $count contains $_\n"; } # kinda golfing print "index: ".$index++." contains $_\n" for @array; __DATA__ 1 2 3 4 5

    cheers

    tachyon

Re: Index in For loop
by jeroenes (Priest) on Jun 27, 2001 at 18:31 UTC
    Besides the other comments I'd like to add:
    Try to rewrite the code so that you don't need the index anymore.

    In many cases, the index isn't needed at all. By leaving out the index, you avoid (1) unneccesary statements (2) complicated so error-prone code. This is not my own idea, I must say, tilly showed me that with my own code. Nowadays I scratch my head a couple of times before using an index-loop.

    Hope this helps,

    Jeroen

Re: Index in For loop
by c-era (Curate) on Jun 27, 2001 at 17:53 UTC
    How about this:
    for my $i (0 .. $#list){ print "index = $i"; print "value = $list[$i]"; }
Re: Index in For loop
by clintp (Curate) on Jun 27, 2001 at 17:54 UTC
    No it does not. It's been proposed, and generally considered Not Important enough to hack in. I do think it's part of the proposal for Perl 6.

    There are lots of other ways to accomplish this, as I'm sure followup posters will demonstrate...

Re: Index in For loop
by wog (Curate) on Jun 27, 2001 at 17:56 UTC
    At this time perl does not allow access to the index within a for loop. There have been suggestions that this be allowed in the past, often centering around giving the deprecated $# variable with this function. $; does not give line numbers. The $. variable does however. See perlvar for all your curiousities about perl's special variables.
Re: Index in For loop
by Abigail (Deacon) on Jun 28, 2001 at 02:35 UTC
    That's why Perl has two forms of for. The one iterating over an array, as you used (and is usually used under its name foreach), and the C-style for:
    for (my $i = 0; $i < @list; $i ++) { print "$i: $list[$i]\n"; }

    -- Abigail

(Correction): Index in For loop
by perchance (Monk) on Jun 27, 2001 at 17:57 UTC
    Sorry for the typo, I did mean the $. variable for files.

    perchance