Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Seeking explanation on assigning a hash to a scalar

by jesuashok (Curate)
on May 15, 2007 at 03:20 UTC ( #615449=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

monks,

#!/usr/bin/perl use strict; use warnings; my %hash = ( 'ONE' => 'one', 'TWO' => 'two' , 'THREE' => 'three' , 4 = +> 'four' , 5 => 'Five' ); my $result = %hash; print "$result\n"; Output :- 4/8
what does the output mean ? should we not assign the hash like that, since there is no meaning of it ?

I am very sorry If my poor English annoyed you all.

Comment on Seeking explanation on assigning a hash to a scalar
Download Code
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 johngg (Abbot) 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

Re: Seeking explanation on assigning a hash to a scalar
by Fletch (Chancellor) 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$..$/

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-08-30 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls