# this is what your first loop does: # it checks the input and brings it into a certain order sub filter_by_array { my ($data,$filter) @_; my $valid = join '|', map quotemeta, @$filter; $valid = qr/$valid/; my %position = do { my $i=0; map {($_=>$i++)} @$filter } my @result = (); foreach( @$data ){ $result->[ $position{ $_ } ] = $_ if /$valid/ } @result } # this is your second piece of code # it checks if the second array completely equals # the corresponding leading elements of the first # array. sub array_pattern { my ($l,$r) = @_; @$l >= @$r or return; for( 0.. @$r-q ){ $l->[$_] eq $r->[$_] or return; } } # validate the input into a certain order my @validated = filter_by_array( \@ARGV , [qw(peace child free love enjoy)] ); # the data you get from somehwere my @car = from_wherever_youlike(); # verify that @car at has all elements of @validated # at its beginning print array_pattern( \@car , \@validated ) ? 'FOUND!' : 'NOT found!' , "\n";