Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: I need some wisdom. How do I pack an array to a set length.

by mr.nick (Chaplain)
on Nov 23, 2011 at 20:28 UTC ( [id://939732] : note . print w/replies, xml ) Need Help??


in reply to I need some wisdom. How do I pack an array to a set length.

The "x" operator works with arrays lists as well:
@b=qw(h o r s e ); $x=4; push @a,$x,((@b) x $x); print join(",",@a);
Edit: /nods to Tux; yeah, sometimes I forget my terminology.

mr.nick ...

Replies are listed 'Best First'.
Re^2: I need some wisdom. How do I pack an array to a set length.
by Tux (Canon) on Nov 24, 2011 at 07:39 UTC

    Though you're example is perfect, your wording is not. x works also on lists, not on arrays:

    $ perl -E'@x=("a".."c");@y=@x x 3;say@y' 333 $ perl -E'@x=("a".."c");@y=(@x)x 3;say@y' abcabcabc $

    That is why the extra parens are needed, otherwise the array would be evaluated in scalar context by x and hence returns its length.


    Enjoy, Have FUN! H.Merijn