johnvandam has asked for the wisdom of the Perl Monks concerning the following question:
Call me silly, but I'm frustrated a bit with the following:
I would like to make an array of say, a thousand elements each filled with 0. What would be the fastest, cleanest way of doing this?
For a string we can replicate a substring:
$string = 0 x 1000;This will give me a string of a thousand 0's.
For making an array in similar fashion this is the shortest method I can think of:
$_ = 0 foreach (@array = (1..1000));I could also use split:
@array = split //, 0 x 1000;But this I think will be more costly because you invoke the regexp engine, I guess.
So my question is: Is there some infinitely better, more perly method of making an array of predefined length with identical values? (Comparable maybe to the more satisfying way of doing something similar for a string?)
Thanks for all the help!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Making an array of predifined length with default values
by almut (Canon) on Mar 04, 2009 at 11:53 UTC | |
by AnomalousMonk (Archbishop) on Mar 04, 2009 at 14:36 UTC | |
by johnvandam (Acolyte) on Mar 04, 2009 at 12:12 UTC | |
|
Re: Making an array of predifined length with default values
by derby (Abbot) on Mar 04, 2009 at 11:54 UTC | |
|
Re: Making an array of predifined length with default values
by clueless newbie (Curate) on Mar 04, 2009 at 11:59 UTC |