Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Is there an equivalent $. for lists or arrays in map or foreach?

by bsdz (Friar)
on Sep 16, 2004 at 16:49 UTC ( #391506=perlquestion: print w/replies, xml ) Need Help??

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

Is there the equivalent of $. ($INPUT_LINE_NUMBER) for lists or arrays that can be used in a map or foreach construct, e.g.
my @f = ('%s', '%.2f', '%u','%d'); my @d = ('my string', '123456.54321' ,'1234.4321' , '1234'); my @D = map { sprintf($f[$LIST_INDEX], $_) } @d; print @D,"\n";
I would like something like $LIST_INDEX to exist. Is there an alternative concise way of doing what I would like to do?

Replies are listed 'Best First'.
•Re: Is there an equivalent $. for lists or arrays in map or foreach?
by merlyn (Sage) on Sep 16, 2004 at 16:55 UTC
      that's precisely what I was hoping for, the following change makes the original code work.
      my @D = map { sprintf($f[$_], $d[$_]) } 0..$#d;
      Thank you
Re: Is there an equivalent $. for lists or arrays in map or foreach? (mapcar!)
by tye (Sage) on Sep 16, 2004 at 18:36 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://391506]
Approved by davido
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2019-07-21 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (7 votes). Check out past polls.

    Notices?