tags:

views:

80

answers:

3

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!);

+12  A: 
@newlist = (@genotypes, ('TT') x 20);

Yes, it is an x.

See Multiplicative Operators in perldoc perlop.

eumiro
+1  A: 

The repetition operator is the most obvious way.
You could also use map:

@newarray = (@genotypes, map 'TT', 1..20);
eugene y
+1  A: 

There's also the foreach way of pushing multiple identical values to an array:

push @newarray, 'TT' foreach (1..20);
plusplus
And the shorter form `push @newarray, 'TT' for (1..20);`
drewk