Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Loading multiple identical variables in array with multiplication variable

by LanX (Archbishop)
on Sep 24, 2014 at 16:45 UTC ( #1101830=note: print w/replies, xml ) Need Help??


in reply to Loading multiple identical variables in array with multiplication variable

Yes, surround the strings with parens to enforce list repetition ( vs string)

edit

DB<126> @arr = (("A") x 2, ("T") x 2, ("C") x 3, ("G") x 3); => ("A", "A", "T", "T", "C", "C", "C", "G", "G", "G")

update

see perlop#Multiplicative-Operators "repetition operator" for details

Binary "x" is the repetition operator. In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in parentheses or is a list formed by "qw/STRING/", it repeats the list. If the right operand is zero or negative, it returns an empty string or an empty list, depending on the context.

but I have to admit it sounds confusing ... :)

update

hmm should maybe be rephrased:

DB<129> $arr = ("G") x 3; => "GGG"

always a string in scalar context!

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Replies are listed 'Best First'.
Re^2: Loading multiple identical variables in array with multiplication variable
by Jeri (Scribe) on Sep 24, 2014 at 16:53 UTC
    perfect thanks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2019-05-22 11:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (139 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!