in reply to One liner to Check and undef hash elements

Update: The first line is why this node is garnishing negative XP. Its wrong for a few reasons..both reasons are within the unless...

First off unless will only evaluate to true if the EXPR inside returns something other than '0', an empty sting '', or undef

Second is the NULL. In this context I was attempting to use it as a alias for '0' which is what perl does, but that was wrong because if we do 'unless( values(%hash) );' that works as well. If we had applied 'use strict;' to this code it would have broke as NULL is a bareword in this situation.

Justifications:
First: I've been coding a bunch of C lately as should be readily apparent to whomever knows C in the bunch. I just didn't fully context switch to perl when whipping out this code.
Second: There was a thread ( I will attempt to find and link to) which raised an interesting situation where when calling values on a newly created hash ( cant remember if it was actually a ref to a hash or not), the values would be autovivified. In that situation I ended up suggesting doing 'unless( join('', values %hash) );' which was the only way to consistantly check to see if the hash really had anything in it.

Sorry for the bad code, but I guess we can stop downvoting this node, and simply use it as an example for .... something somewhere I'm sure..
# will print "no keys" if all values are undef.. print "no keys\n" unless( values(%hash) != NULL ); # to resest a hash.. %hash = (); # or if you are paranoid undef(%hash) && %hash = ();
Happy hacking

MMMMM... Chocolaty Perl Goodness.....

Replies are listed 'Best First'.
•Re: Re: Checking and undef'in hash elements in one step?
by merlyn (Sage) on Apr 16, 2003 at 03:11 UTC
      Perhaps saying that NULL isn't a keyword would be more constructive, or that he meant undef instead.
      cb reference!:
      now, what are the odds I log in in the morning to find all my posts mysteriously downvoted?
      UPDATE:Awww. not downvoted at all.

      feanor_269
      It was more to display the idea .. but its syntax is correct and in testing provided acurate results.. I've now garnished quite a few negative XP for this nodelet.. Thanks for at least responding to the post...

      Now why dont people like this??

      should it have said "no values were defined?" which matches what was asked for?.. should I have left it as unless( values(%hash)) ???

      after testing some more I now see that the values(%hash) is sufficient.. I stand corrected and accept the negative XP..
      /me dohs and hangs his head in shame...

      MMMMM... Chocolaty Perl Goodness.....
      A Perl line with syntax error is still Perl.

      To be frank, I don't see the point of your last line, that Q&A. It should not be too difficult for people to be nice to each other. Make pointless comment (I point to your last line) is an insult to the merit of the poster, and the community at large. I don't see this funny at all.

      I read your disclaimer, but it does not entitle you to be rude, or gives you the excuse to be rude. Doesn't matter whether it is for a good purpose or a bad one, the way you expressed yourself is rude, and there is no need to put things in that way.

      I point out this to you for your benefit, to be sincere. I don't jump on you because of this single incident, but you have been doing this to people from time to time.

      Well, I understand how you behave yourself is really your decision.

      As you said the syntax is wrong, let me tell you now, from a pure syntax point of view, this is perfectly correct Perl syntax.

      Didn't you realize that NULL could be a constant?
      use strict; use constant NULL => ""; my @a = (1,2,3); my @b = (); my @c = (""); print "a is empty: " . is_null(@a), "\n"; print "b is empty: " . is_null(@b), "\n"; print "c is empty: " . is_null(@c); sub is_null { my @a = @_; (@a == NULL) ? "true" : "false"; }