Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: List Compare

by dragonchild (Archbishop)
on Oct 21, 2005 at 14:26 UTC ( #502008=note: print w/replies, xml ) Need Help??


in reply to List Compare

Assuming you're working with just strings, something like the following would work:
my @a = 'a' .. 'c'; my @b = 'a' .. 'f'; my $compare = do { my $temp = join '.*?', @a; qr/$temp/; }; sub compare { my @b = @_; my $temp = join '', @b; return $temp =~ $compare; }

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: List Compare
by inman (Curate) on Oct 21, 2005 at 14:32 UTC
    You beat me to it!! I had it the other way round though as per the OPs example. The joined @a is only calculated once and then matched against the regex made from joining @b with .*?
    my @a= qw (a b c d e f); my @b= qw (a b c); my $joined_a = join '', @a; my $joined_b = join '.*?', @b; print "yes!\n" if $joined_a =~ /$joined_b/;

    But I guess there are no prizes for coming second...

Re^2: List Compare
by sauoq (Abbot) on Oct 21, 2005 at 20:11 UTC

    This... uh... doesn't meet your first criteria for good software. (And it didn't even before he updated his spec.)

    Set @b = qw( a foobar c ) to see it fail.

    You might get away with something like that by including some "\0"s in your join but I certainly would not recommend it. Even before he added the additional constraint that there could be no missing elements, this would be easily solved with a single loop.

    -sauoq
    "My two cents aren't worth a dime.";
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://502008]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2018-07-19 02:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (398 votes). Check out past polls.

    Notices?