we often use something like that:
my $count=0; foreach my $foo (@bar) { print $count . "-> " . $foo; $count++; }
wouldn't it be cool have a special
variable counting the loop ? maybe something like that:
foreach my $foo (@bar) { print $~~ . "-> " . $foo; }
"$~~" isn't as cool as it should be
maybe there is better.

i know, i am a lazy yaph
or what do you think ?

Replies are listed 'Best First'.
Re: foreach count variable
by rob_au (Abbot) on Nov 18, 2002 at 21:42 UTC
    As it is, there is currently no implicit variable available to serve your purpose - There was the proposal of such a variable made in RFC120 and RFC262 for Perl 6 but this has been rejected by Larry - See Apocolypse 4, specifically here and here.

    The only way to implement such a counter is to loop by index, rather than value - For example:

    foreach my $count ($[..$#bar) { print $count, " -> ", $bar[$count], "\n"; }

    Not quite as pretty or flexible, but the introduction of implicit loop variables would also mean the introduction of additional construct overhead.

     

    perl -e 'print+unpack("N",pack("B32","00000000000000000000000111100100")),"\n"'

Re: foreach count variable
by Zaxo (Archbishop) on Nov 18, 2002 at 21:34 UTC
    $. does that for the special case of iterating over records from a filehandle.

    Update: Ooh! Just made a connection -

    my $bar = join $/, @bar; $bar .= $/; open my $foo, '<', \$bar or die $!; while (<$foo>) { print $., ' -> ', $_; } close $foo or die $!;
    That will take perl 5.8 to work. Yes, it's much heavier than $count++, but it's a window into a whole 'nother thing. (Added shameless plug) See Open Has a New Trick.

    After Compline,
    Zaxo

Re: foreach count variable
by BrowserUk (Patriarch) on Nov 18, 2002 at 21:45 UTC

    for my $count (0..$#bar) { print $count . "-> " . $bar[$count]; }

    In someways better than using for my $thing (@array){...} as n..m evaluates its list lazily, so doesn't build a huge list from @array just to consume it one thing at a time.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: foreach count variable
by gjb (Vicar) on Nov 18, 2002 at 21:32 UTC

    Okay, many are going to flame me, but this looks like an excellent occasion to use a C-type loop:

    for (my $count = 0; $count < scalar(@bar); $count++) { printf "%d -> %s\n", ($count, $bar[$count]); }

    Awaiting your scorn, -gjb-

      You needn't flinch, there's nothing wrong with that.

      An alternative is:

      my @foo = qw(one two three four); for (0..$#foo) { print "$_ : $foo[$_] \n"; }

      Andy.

      yeah, thats the other way doing it.
      but still too much keys to press,
      and it looks quite 'untidy' :-)
      Your idea is fine, but the execution could be better.. :) There's no need for scalar nor printf there.
      for (my $count = 0; $count < @bar; $count++) { print "$count -> $bar[$count]\n"; }
      But then, I'd still prefer the for(0 .. $#bar) form proposed further down the thread.

      Makeshifts last the longest.

Re: foreach count variable
by dreadpiratepeter (Priest) on Nov 19, 2002 at 05:10 UTC
    I've also always wanted that feature. Sometimes you want both the element and the position at the same time.
    I talked to Damian and Dan about it at the Perl conference year before last and they liked my suggestion (for Perl6) but I guess nothing ever came of it.
    I proposed an attribute on the topic (list variable), ie:
    foreach my $foo qw(a b c d e f g) { print $foo,$foo.index; }

    Although I'm not sure that index is the best name for the attribute. Maybe position?

    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
      yep. in perl6 this attribute would be nice.
      there is even one more way to go:
      foreach my $foo (@bar) count $c { print $c . "-> " . $foo; }
      or maybe:
      foreach my $foo (@bar) counting $c { print $c . "-> " . $foo; }