There are many ways you can insert data into an array:
You can also insert at the ends with simple assignment:
@T = ( 'FIRST', @T, 'LAST' );Reassigning with slices can also do the trick:
@T = (@T[0..50], 'FOO', 'BAR', @T[51..$#T]);I frequently find myself rebuilding lists into new lists as I process items:
my @tmp; for my $item (@T) { if (..insert condition..) { push @tmp, 'Scooby Doo!'; } if (..keep condition..) { push @tmp, $item; } } @T = @tmp;
Which method(s) you choose depend on which is most convenient in the code you're writing.
Notes: None of these have been tested, and the ...blah.. need to be replaced with appropriate expressions.
...roboticus
When your only tool is a hamster, all problems are short-lived.
R.I.P. Hammy ~1970 .. 1972
In reply to Re: Insert Element Into Array
by roboticus
in thread Insert Element Into Array
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |