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

Is there a special variable that holds the index to the current element for a map? Or any other way besides a global variable to keep a running count in a map? I'm hoping something exists for map like @- for regexps, but I can't find it if it does.

--Solo
--
You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

Replies are listed 'Best First'.
Re: Running count in map
by diotalevi (Canon) on Aug 06, 2004 at 18:09 UTC

    dragonchild has it though you'll have to ignore the confusing way he said it. You don't need a global, just a variable that is accessible. my()'d variables are perfectly acceptable here.

    my $counter = 0; ... map { ++$counter; ... } ...
Re: Running count in map
by Prior Nacre V (Hermit) on Aug 06, 2004 at 18:11 UTC

    You can do something like:

    my @map_list = ( ... ); map { process $map_list[$_] } (0 .. $#map_list)

    Regards,

    PN5

Re: Running count in map
by dragonchild (Archbishop) on Aug 06, 2004 at 18:04 UTC
    map doesn't maintain state, just like grep doesn't. They operate on lists, not items in the list.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      Of course $_ is an alias for the original items in the list so you can use it to modify the original items if you want.</pedant> (not that that answers the original is-there-an-index question)