in reply to comparing values between two arrays

merlyn and broquaint beat me to it with much better solutions, but here is my effort for what its worth.

Regards,
Dom.

use strict; use warnings; use Data::Dumper; my @numbers = ('25', '12', '32','56','45','21','65'); my @needed = ('25','32','45','65'); my ($iNeeded, %hashNeeded, $iNumber, @result); my $bWithinMatch = 0; foreach $iNeeded (@needed) { $hashNeeded{$iNeeded} = 1 } foreach $iNumber (@numbers) { #Are we within a match? if ($bWithinMatch) {push @result, $iNumber} #Is this a needed number? if (defined $hashNeeded{$iNumber}) { #Change state $bWithinMatch = not $bWithinMatch; #If this is the start of a match, store the result if ($bWithinMatch) {push @result, $iNumber} } } print Dumper(\@result);
Update: