Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to test for empty hash?

by perlfan (Vicar)
on Aug 08, 2021 at 02:28 UTC ( #11135675=note: print w/replies, xml ) Need Help??


in reply to How to test for empty hash?

I came here to suggest scalar, as in,
perl -e 'my %x=(); print scalar %x' 0

but then, this caused me pause:

perl -e 'my %x=undef; print scalar %x' 1/8

Replies are listed 'Best First'.
Re^2: How to test for empty hash? (Perl Secret Operators)
by eyepopslikeamosquito (Bishop) on Aug 08, 2021 at 06:01 UTC
      With all this talk of scalar, I had to look it up because, despite using Perl heavily for twenty years, I've never actually used it! Which reminded me that I have employed the equivalent "secret" version, namely the infamous inchworm ~~ secret operator

      Just a little nitpick: the "inchworm" isn't exactly equivalent, as it doesn't pass through lvalue context, which scalar does as of 5.22.

      $ perlbrew exec perl -e 'for(scalar($#foo)) { $_=3 } warn "$] ".@foo." +\n"' >/dev/null 5.034000 4 5.032001 4 5.030003 4 5.028003 4 5.026003 4 5.024004 4 5.022004 4 5.020003 0 5.018004 0 $ perlbrew exec perl -e 'for(~~$#foo) { $_=3 } warn "$] ".@foo."\n"' > +/dev/null 5.034000 0 5.032001 0 5.030003 0 5.028003 0 5.026003 0 5.024004 0 5.022004 0 5.020003 0 5.018004 0

      Update: The example above is one of the exceptions for when ~~ is not equivalent to scalar anyway:

      $ perl -le 'print scalar($#foo)' -1 $ perl -le 'print ~~$#foo' 18446744073709551615

      ... but the point still stands (though interestingly, my $foo="x"; for(scalar($foo)) {$_.="y"} worked even before 5.22).

        Impressive analysis!

        It doesn't surprise me there are subtle differences. I only mentioned secret operators for some light relief, and (unlike BooK ;), would never inflict the inchworm secret operator on my workmates in production code ... my rule number one at work being "Correctness, simplicity and clarity come first. Avoid unnecessary cleverness".

        Curiously, your second example seems to be fixed by adding use integer:

        $ perl -le 'print ~~$#foo' 18446744073709551615 $ perl -Minteger -le 'print ~~$#foo' -1

        ... which reminds me of this recent reply by BillKSmith ... while typing -Minteger from the command line just now reminded me of chromatic's comical -Modern::Perl command line hack. :)

        Update: See also: the "Inchworm" and "Inchworm on a stick" sections at perlsecret and The Lighter Side of Perl Culture (Part IV): Golf (search for inchworm).

Re^2: How to test for empty hash? (updated)
by AnomalousMonk (Bishop) on Aug 08, 2021 at 03:24 UTC

    No cause for pause. undef is promoted to the empty string, used as a hash key and assigned undef as its value because that's the default value of items in the empty list | used by Perl when a key in a hash assignment list has no corresponding value, or Odd number of elements in hash assignment.

    Win8 Strawberry 5.8.9.5 (32) Sat 08/07/2021 23:15:45 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dump qw(dd); my %h = undef; print scalar %h, "\n"; dd \%h; ^Z Odd number of elements in hash assignment at - line 7. Use of uninitialized value in list assignment at - line 7. 1/8 { "" => undef }
    The hash ends up with 1 key/value pair.

    Update: Clarified (I hope!) the explanation of odd-item hash assignment.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2022-05-19 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?