for (my $i=0; $i<@numbers; $i++) { # psudocode # # if $numbers[$i] matches a value in @needed; # push @found, all the numbers until the next match for (my $k = 0; $k < @needed; $k++) { if ($numbers[$i] == $needed[$k]) { push @found, $numbers[$i]; # not sure how to carry on until the next match! } } } }