in reply to sublist of a list

It's cheap, but the first thing I thought of was to serialize the lists and use index to see if one is a substring of the other. Something like...

sub serialize { my @copy = @_; foreach my $o ( @copy ) { $o =~ s/\\/\\\\/g; # escape escape char $o =~ s/,/\\,/g; # escape delimiter } return join q{,}, @copy; } sub in_list { my ( $la_ref, $lb_ref ) = @_; my @la = @{$la_ref}; my @lb = @{$lb_ref}; return ( !@la || 0 <= index serialize( @lb ), serialize( @la ) ); }

Replies are listed 'Best First'.
Re^2: sublist of a list
by ikegami (Patriarch) on Nov 02, 2008 at 21:35 UTC

    Two bugs.

    in_list([','], ['a\\', 'b']) returns true. It should return false.

    in_list([qw( 1 2 4 )], [qw( 1 2 3 4 )]) returns false. It should return true since the OP is searching for a subsequence, not a substring.

    Update: My fix for the first bug was equally buggy. Removed.