[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Does a hash contain any keys other than <LIST>?



>>>>> "JP" == John Porter <jdporter@min.net> writes:

JP> Chaim wrote:
PS> want to know just whether it contains any keys *other* than those listed 
PS> in, say, @k. 
>> 
>> How about:
>> 
>> join(':',sort keys %h) eq join(':', sort @k)
>> or
>> @t{@k, keys%h}=();
>> scalar(keys %t) != scalar(@k);

JP> But that fails for the case where %h doesn't have some of the keys
JP> listed in @k.  Probably important; at least, Peter's solution
JP> doesn't fail that way.

Why does it fail? The problem is finding a key that IS NOT in the
original list, i.e. Are there any unexpected or extra keys.

	@k = (a,b,c);
	%h = (c=>1,d=>1);

after the assignment
	keys %t is (a,b,c,d);

Are there any keys that shouldn't be there?

	keys(%t) == 4 
	scalar(@k) == 3

Yes, there are unexpected keys.

Lets try another case:

	@k = (a,b,c)
	%h = (a=>1)

After it all
	keys(%t) = 3
	scalar(@k) = 3

All are accounted for.

<chaim>
-- 
Chaim Frenkel					     Nonlinear Knowledge, Inc.
chaimf@pobox.com				               +1-718-236-0183

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org