Just another Perl shrine | |
PerlMonks |
Making an array of predifined length with default valuesby johnvandam (Acolyte) |
on Mar 04, 2009 at 11:43 UTC ( [id://748083]=perlquestion: print w/replies, xml ) | Need Help?? |
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: 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!
Back to
Seekers of Perl Wisdom
|
|