Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: List Compare

by BrowserUk (Pope)
on Oct 21, 2005 at 14:51 UTC ( #502020=note: print w/replies, xml ) Need Help??


in reply to List Compare

I'm not sure that 'leftSubset' is the best name, but it should work for any type of values. Though, if you want '2' to compare equal to '2.0', you'll need to tweak it.

It takes a references to the two arrays, the master first, the variable array second, and returns true false.

#! perl -slw use strict; use List::Util qw[ first ]; sub leftSubset{ local $^W; my( $major, $minor) = @_; return @{$minor} == first{ $major->[ $_ ] ne $minor->[$_] } 0 .. @{$minor} } my @a = 'a' .. 'f'; print "\n@$_\n is left subset of \n@a: ", leftSubset( \@a, $_ ) ? 'YES' : 'NO' for ['a'..'c'], ['b'..'d'],[qw[a a b c]],[ reverse 'a'..'c'];; __END__ P:\test>junk2 a b c is left subset of a b c d e f: YES b c d is left subset of a b c d e f: NO a a b c is left subset of a b c d e f: NO c b a is left subset of a b c d e f: NO

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://502020]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2018-11-15 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (186 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!