in reply to List Compare
is probably not construable (depending on your exact view of the requirement) as being an ordered subset of:@b = (ab, c);
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:@a = (a, bc, d);
update: factorised to reduce a couple of linessub 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 ) ); }
-M
Free your mind
|
|---|