Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: scanning hash

by Aristotle (Chancellor)
on Aug 01, 2004 at 14:56 UTC ( #379082=note: print w/ replies, xml ) Need Help??


in reply to Re^2: scanning hash
in thread scanning hash

You'll have to extend that a bit so it properly deals with undef vs empty string.

sub ident { my $value = ( values %{ $_[0] } )[0]; for ( values %{ $_[0] } ) { no warnings 'uninitialized'; return 0 unless !( defined $value xor defined $_ ) and ( $valu +e eq $_ ); } return 1; }

It would be better if I could think of a way to skip the $value eq $_ test when both variables are undefined, and still have the entire expression be true, but I can't see a way to do that without an extra test for definedness on one of the values.


Update after bageler's reply: this was wrong:

return 0 unless ( defined $value xor defined $_ ) and ( $value eq $_ ) +;

The condition will only ever be true when comparing an undef with an empty string, but in no other case, because the left expression is only true if the operands are unequal, ie if only one of them is defined. In that case, the right expression can only also be true if the defined value is an empty string.

Makeshifts last the longest.


Comment on Re^3: scanning hash
Select or Download Code
Re^4: scanning hash
by bageler (Hermit) on Aug 01, 2004 at 15:06 UTC
    need to change that slightly so it will return true if all the keys are undef:
    return 0 unless defined $_ ? $_ eq $value : defined $value ? 0 : 1

      No, I don't.

      $ perl -le'print undef eq undef' 1

      Update: oh, I see what made you think that. I had to NOT my XOR condition. I forgot that XOR produces false rather than true if both operands are equal.

      Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-08-20 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (111 votes), past polls