in reply to Making an array of predifined length with default values

my @array = (0) x 1000;

Even works with lists of arbitrary length, such as my @array = (1, 2, 3) x 100;  (which creates 300 elements).

(but memory-wise, it's not better than iteratively filling the elements, maybe even with predeclaring some initial max size with $#array = 1000; to avoid successive automatic resizing...)

Replies are listed 'Best First'.
Re^2: Making an array of predifined length with default values
by AnomalousMonk (Archbishop) on Mar 04, 2009 at 14:36 UTC
    Just a very minor nit: the statement  $#array = 1000; sets the maximum array index to 1000 and so predeclares an array with 1001 elements (assuming $[ == 0; see perlvar).
Re^2: Making an array of predifined length with default values
by johnvandam (Acolyte) on Mar 04, 2009 at 12:12 UTC
    Thanks, this works perfectly! I though it only worked for strings. Thanks again