> Is there a nice clean perl way to test to see if only one key of a hash
> has data?
> 
> %some_hash = ( key1 => 1, key2 => 2 ); should fail the test
> %some_hash = ( key2 => 2 ); should pass the test
> %some_hash = ( key1 => 1 ); should pass the test
> %some_hash = ( key1 => 1, key2 => 2, key3 => 3 ); should fail the test
> %some_hash = ( key2 => 2 ); should pass the test
> %some_hash = ( key1 => 1 ); should pass the test
> %some_hash = ( key2 => 2, key3 => 3 ); should pass the test
> %some_hash = ( key1 => 1, key3 => 3 ); should pass the test
> 
> Basically I can many more keys in the hash but two of the keys have to
> be exclusive.
> 

You really could have said that a LOT better...  It took several
re-readings, and finally "what's the difference between test-cases 1,
7 and 8?" to understand what you meant.

How 'bout 

Given a hash %hash with many keys ( qw / key1 key2 key3 keyn / ) 
how do I test that only one of the values $hash{key1} or $hash{key2} are 
defined?

if ( exists $hash{key1} and exists $hash{key2}  ) {
   warn "fail";
} else { 
   warn "succeed";
}

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
        Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g
Computer  software  consists of  only  two  components: ones  and
zeros, in roughly equal proportions.   All that is required is to
sort them into the correct order.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to