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

Answers


@newlist = (@genotypes, ('TT') x 20);

Yes, it is an x.

See Multiplicative Operators in perldoc perlop.


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

Need Your Help

Is the Writer Monad effectively the same as the State Monad?

scala monads scalaz state-monad writer-monad

There's a great tutorial here that seems to suggest to me that the Writer Monad is basically a special case tuple object that does operations on behalf of (A,B). The writer accumulates values on...

Chart.js x-axis

javascript chart.js

I was wondering if there was a way to make the X-Axis labels in chart.js increment by a certain scale.