Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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.

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://379082]
erix .oO( note to self: running 100 syncing database instances on a single machine is hard on the hard disks )
[1nickt]: Oh, no, sins of the flesh were *especially* verboten at that monastery!

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2017-12-13 13:23 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (367 votes). Check out past polls.