Alex Rivera | Logout

How can I add repeated values to an array in Perl?

Asked 2010-10-05T14:06:33.827
24

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

Edit
Report

1 Answer

4

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

push @newarray, 'TT' foreach (1..20);
answered 2010-10-05T14:39:15.820

Your Answer