The pitfall that is hard to avoid when using the join-to-form-a-regexp solution is that, for example:
@b = (ab, c);
is probably not construable (depending on your exact view of the requirement) as being an ordered subset of:
@a = (a, bc, d);
My first reaction was therefore to use the standard join( $;, array ) approach ($; is a non-printable field separator often used for this purpose), while mulling over the possibility that the data might be binary and so accidentally contain a byte equal to $; (it's the ascii of decimal 19 and reassigning $; would just move the problem to a different false-positive), the following non-regexp approach drifted into my head - it does at least do about the minnimum necessary while being safe from the above join-delimitation problem:
sub isOrderedSublist{ my ( $aref, $bref ) = @_; # references to arrays my @a = @$aref; # major - copy so we can safely destroy my @b = @$bref; # minor my $firstMatched = 0; while( not Empty( \@a ) ) { if ( $a[0] eq $b[0] ) { $firstMatched = 1; shift @b; else { $firstMatched and last; } shift @a; } return Empty( \@b ); # success if failure eliminated } sub Empty{ my $aref = shift; return( not ( $#$aref + 1 ) ); }
update: factorised to reduce a couple of lines

-M

Free your mind


In reply to Re: List Compare by Moron
in thread List Compare by anniyan

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.