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

Hello fellow monks!

I have a $number variable, and a $string, what is the fastest way to create a list of size $number that holds $string in each list entry? for example:
$number = 4; $string = 'hello'; # I want to get the list: @list = qw(hello hello hello hello);
Thanks

Hotshot

Replies are listed 'Best First'.
Re: Array population
by broquaint (Abbot) on Apr 08, 2003 at 15:23 UTC
    I have a $number variable, and a $string, what is the fastest way to create a list of size $number that holds $string in each list entry?
    Why by using the repetition operator in a list context of course
    my $number = 4; my $string = 'hello'; my @list = ($string) x $number; print @list; __output__ hellohellohellohello
    See. the Multiplicative Operators section of perlop for more info on the repetition operator.
    HTH

    _________
    broquaint

Re: Array population
by Abigail-II (Bishop) on Apr 08, 2003 at 15:24 UTC
Re: Array population
by cbro (Pilgrim) on Apr 08, 2003 at 15:29 UTC
    my $number = 4;
    my $string = "hello";
    my @array = ($string) x 4;
    Sorry, replace '4' with $number...oops