Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://939732]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-23 22:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found