in reply to Is one array a subset of the other
It first defines hashkeys for all elements from @a2, then removes all haskeys from @a1. If any haskey remains, @a2 had elements not present in @a1.@a1=qw(a b c d e f g); @a2=qw(b g); @a3=qw(e f g h); print "a1, a2 => ",subset(\@a1, \@a2),"\n"; print "a1, a3 => ",subset(\@a1, \@a3),"\n"; sub subset { my($a1, $a2)= @_; my(%subset); @subset{@$a2}=@$a2; delete @subset{@$a1}; return scalar(keys %subset)==0; }
|
|---|