# 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";

I'm all the time wondering: what do you need this for. It doesn't make much sense (to me).

--
http://fruiture.de

In reply to Re: terrible code about array matching... by fruiture
in thread terrible code about array matching... by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.