in reply to Re: add elements to array at certain positions
in thread add elements to array at certain positions
$i should increment each time or else the value in @a that we didn't want will still be printed after the 'I'
my $i = 0; my @new = map { ++$i; /^[yz]$/ ? 'I' : $a[$i] } @b;
A solution I thought of with List::MoreUtils:
use List::MoreUtils qw(pairwise); my @mine = pairwise { $b =~ /^[yz]$/ ? 'I' : $a; } @a, @b;
Mine is not as nice because it doesn't work properly if both arrays are not the same size. You would have to add a check for undefined values or pad either array to match the other array's size. Perl also warns that $a and $b are only used once.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: add elements to array at certain positions
by GrandFather (Saint) on Oct 30, 2008 at 06:55 UTC | |
by juster (Friar) on Oct 30, 2008 at 16:56 UTC |