How can I add repeated values to an array in Perl?
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!);
@newlist = (@genotypes, ('TT') x 20);
Yes, it is an x.
The repetition operator is the most obvious way. You could also use map:
@newarray = (@genotypes, map 'TT', 1..20);
There's also the foreach way of pushing multiple identical values to an array:
push @newarray, 'TT' foreach (1..20);