use warnings; use strict; my @a = qw(0 1 5 7 9 5 3); my @b = qw(a b c y e f z a a); my @new; while (@a && @b) { my $nextB = shift @b; if ($nextB =~ /[yz]/) { push @new, 'I'; } else { push @new, shift @a; } } print "@new";