Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Initialization of arrays

by sk (Curate)
on Sep 22, 2005 at 14:02 UTC ( #494120=note: print w/ replies, xml ) Need Help??


in reply to Initialization of arrays

chester and svenXY have told you why it does not do what you want and how you can init the slice. Here is a quick way to init if you want to do it for a large number of indices.

use strict; use warnings; use Data::Dumper; my @x; @x[0..3] = map {1}0..3; # this map returns a list of 1's (4 elements) print Dumper(\@x); __END__ $VAR1 = [ 1, 1, 1, 1 ];

BTW, you had @x[1..3] I changed it @x[0..3]. I felt that is probably what you want as the array index starts with zero. If you are sure you don't need zero then you can modify my code.

-SK


Comment on Re: Initialization of arrays
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-09-19 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (138 votes), past polls