Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Filling an array

by spartan (Pilgrim)
on Sep 07, 2001 at 02:00 UTC ( #110761=note: print w/replies, xml ) Need Help??

in reply to Filling an array

I was inspired by your map example to do something like this instead:
$value=" 1 "; @array=map {$_=$value} (1..100); $i=0; foreach (@array) { print "$i $_\n"; $i++; }
The $i variable (used as an incrementer) and the foreach merely proves the map statement.

Very funny Scotty... Now PLEASE beam down my PANTS!

Replies are listed 'Best First'.
(zdog) Re: (2) Filling an array
by zdog (Priest) on Sep 07, 2001 at 04:27 UTC
    You can eliminate the $i var by doing the following:

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

    Also, I would think you'd be better off using one of the other suggestions like for() or the x operator rather than map() for efficiency reasons.

    Zenon Zabinski | zdog |

      for (0 .. $#array) { print "$_ $array[$_]\n" }
      I like that... very clean, to the point, and quite readable. The only reason I chose map was to flex an all too atrophied brain muscle. :)

      Very funny Scotty... Now PLEASE beam down my PANTS!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://110761]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2023-02-08 23:35 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (44 votes). Check out past polls.