http://www.perlmonks.org?node_id=615449

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Seeking explanation on assigning a hash to a scalar
by imp (Priest) on May 15, 2007 at 03:52 UTC
    From perldata
    If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash. This is pretty much useful only to find out whether Perl's internal hashing algorithm is performing poorly on your data set. For example, you stick 10,000 things in a hash, but evaluating %HASH in scalar context reveals "1/16", which means only one out of sixteen buckets has been touched, and presumably contains all 10,000 of your items. This isn't supposed to happen
    It is not useful to assign a hash to a scalar. It is useful to store a reference to a hash in a scalar though, e.g.
    my %hash = (foo => 1); my $ref = \%hash; print $ref->{foo};
    This seems like an odd question to ask - are you looking for perl trivia, or trying to solve a problem?
      If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash. This is pretty much useful only to find out whether Perl's internal hashing algorithm is performing poorly on your data set.

      I wrote it before and I'm repeating it here, again without proper attribution (anyone here?) nor the certainty of quoting it really correctly: "the value of a hash in scalar context is of no use to a Perl programmer, only to a perl programmer."

Re: Seeking explanation on assigning a hash to a scalar
by Samy_rio (Vicar) on May 15, 2007 at 03:53 UTC

    Hi jesuashok, It means "Number of used buckets and the number of allocated buckets, separated by a slash." ( Ex.: 4/8 ).

    In \html\lib\Pod\perldata.html, description as,

    If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash. This is pretty much useful only to find out whether Perl's internal hashing algorithm is performing poorly on your data set. For example, you stick 10,000 things in a hash, but evaluating %HASH in scalar context reveals "1/16", which means only one out of sixteen buckets has been touched, and presumably contains all 10,000 of your items. This isn't supposed to happen.

    You can preallocate space for a hash by assigning to the keys() function. This rounds up the allocated buckets to the next power of two:

    keys(%users) = 1000; # allocate 1024 buckets

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Seeking explanation on assigning a hash to a scalar
by Fletch (Bishop) on May 15, 2007 at 12:38 UTC

    The poor English isn't what's annoying. What's really annoying is the constantly posting trivial questions to which you either should know the answers (even with the marginal Perl abilities that you've displayed here previously) or to which the answer should be readily apparent after no more than 5 or 10 minutes of careful reading of the obviously appropriate documentation (or from using an obvious search term here or on google).

      ++Fletch.  I'm in total agreement.

      Now I'll be willing to admit that maybe I was wrong, jesuashok, if you actually respond to Fletch's claim, and explain why you keep constantly posting trivial questions.

      But my money's on your ignoring his question, as usual, and moving on to yet another question which the documents are happy to provide an immediate answer for.  (Update:  Looks like I was correct).

      By the way, a number of times in the past you've made it clear that you're not too happy with getting tons of -- votes.  I submit to you that your wasting time and space here is the main factor.  As a result, unless you have some fabulous reason for continuing these charades, I'll continue to vote you down every time you ask these silly questions.


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Seeking explanation on assigning a hash to a scalar
by johngg (Canon) on May 15, 2007 at 09:07 UTC
    imp and Samy_rio have shown you the documentation that demonstrates that there is a meaning to the result you got. Perhaps you were expecting the assignment of a hash to a scalar to behave in a similar way to assigning an array to a scalar, i.e. give the number of keys and values in the hash. If that was what you wanted you can do that like this

    use strict; use warnings; my %hash = (a => 1, b => 2, c => 3); my $buckets = %hash; my $elems = () = %hash; print qq{ Buckets: $buckets\nElements: $elems\n};

    which prints

    Buckets: 3/8 Elements: 6

    I hope this is of interest.

    Cheers,

    JohnGG