Reputation: 243
I have an array @genotypes = "TT AG TT AG...."
and want to add a spike to it (e.g. 20 x TT) to make a new array.
I can obviously push
"TT" into the array 20 times - but is there a simpler way of doing this? (ie. not @newarray = push @genotypes ("TT", "TT", "TT",......20 times!);
Upvotes: 24
Views: 10423
Reputation: 2030
There's also the foreach
way of pushing multiple identical values to an array:
push @newarray, 'TT' foreach (1..20);
Upvotes: 4
Reputation: 149806
The repetition operator is the most obvious way.
You could also use map
:
@newarray = (@genotypes, map 'TT', 1..20);
Upvotes: 6
Reputation: 212885
@newlist = (@genotypes, ('TT') x 20);
Yes, it is an x
.
See Multiplicative Operators in perldoc perlop.
Upvotes: 43