Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 294
jane Avatar asked Oct 05 '10 14:10

jane


People also ask

How do I push multiple values in an array in Perl?

The push() function pushes the new value or values onto the right side of the array and increases the elements. ); push @myNames, 'Moe'; You can also push multiple values onto the array directly ...

Which function can add values to an array in Perl?

push() function in Perl is used to push a list of values onto the end of the array.

What is array slicing in Perl?

PERLServer Side ProgrammingProgramming Scripts. You can also extract a "slice" from an array - that is, you can select more than one item from an array in order to produce another array.


1 Answers

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

Yes, it is an x.

See Multiplicative Operators in perldoc perlop.

like image 52
eumiro Avatar answered Oct 09 '22 11:10

eumiro