spurperl has asked for the wisdom of the Perl Monks concerning the following question:

I have to init an array with a heap of identical values, and seek for a standard way, w/o a loop. Recently I don't sleep much, so no normal solutions come to my mind, just hacks like this:

my @bytes = split(/ /, "30 " x 25);
The above inits @bytes with 25 copies of "30".

Any other ideas ?

Replies are listed 'Best First'.
Re: better way to init an array ?
by sauoq (Abbot) on Jun 04, 2003 at 07:13 UTC
    my @bytes = (30) x 25;
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: better way to init an array ?
by msemtd (Scribe) on Jun 04, 2003 at 08:03 UTC
    Hmmm, this'll do it...
    @bytes = grep 'nudity', ("30") x 25;
    ...although the use of nudity is gratuitous.
Re: better way to init an array ?
by Skeeve (Parson) on Jun 04, 2003 at 08:10 UTC
    Just to add my 2 cents in this obfuscation contest ;-)
    my @bytes = unpack('C*',"\c^"x25);
Re: better way to init an array ?
by leriksen (Curate) on Jun 04, 2003 at 07:36 UTC
    @bytes = map {30} (0..25);

    not saying its better, just different.
      Very different in that it gives you 26 of them
Re: better way to init an array ?
by Chady (Priest) on Jun 04, 2003 at 09:37 UTC

    No loop used here:

    my @bytes = qw/30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 3 +0 30 30 30 30 30 30/;

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: better way to init an array ?
by DrHyde (Prior) on Jun 04, 2003 at 10:37 UTC