Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > understanding an error

Reply
Thread Tools

understanding an error

 
 
Justin C
Guest
Posts: n/a
 
      11-03-2006
I was trying to count the number of elements in a hash, I realise that
the code below is not the way to do it, I've got that bit sorted now,
what I want to know is how I got the output I did.

Here is the code:

#!/usr/bin/perl

use warnings ;
use strict ;

my %hash = (
"mick" => "jagger",
"brian" => "jones",
"keith" => "richards",
"charlie" => "watts",
"bill" => "wyman",
"rolling" => "stones"
) ;

# my $n_items = scalar ( keys ( %hash ) ) ;
# print $n_items, "\n" ;

print scalar ( %hash ), "\n" ;

# end of code

The output, when this is run, is:
4/8

Where does that output come from?

BTW, I'm counting hash elements: my $n_items = scalar( keys( %hash)); now.

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-03-2006
Justin C <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I was trying to count the number of elements in a hash, I realise that
> the code below is not the way to do it, I've got that bit sorted now,
> what I want to know is how I got the output I did.
>
> Here is the code:
>
> #!/usr/bin/perl
>
> use warnings ;
> use strict ;
>
> my %hash = (
> "mick" => "jagger",
> "brian" => "jones",
> "keith" => "richards",
> "charlie" => "watts",
> "bill" => "wyman",
> "rolling" => "stones"
> ) ;
>
> # my $n_items = scalar ( keys ( %hash ) ) ;
> # print $n_items, "\n" ;
>
> print scalar ( %hash ), "\n" ;
>
> # end of code
>
> The output, when this is run, is:
> 4/8
>
> Where does that output come from?


perldoc perldata, look for the paragraph starting

If you evaluate a hash in scalar context,

Anno
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-03-2006
Michele Dondi wrote:
> Justin C wrote:
>>
>>print scalar ( %hash ), "\n" ;

> [snip]
>>The output, when this is run, is:
>>4/8

>
> As somebody put it, "the value of a hash in scalar context is of
> interest only to a perl programmer, not to a Perl programmer".


Sometimes I do:

if ( %hash ) {
# do something...
}

and I'm certainly not a perl programmer, so "somebody" must be wrong.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      11-03-2006
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de wrote:

> perldoc perldata, look for the paragraph starting
>
> If you evaluate a hash in scalar context,


Hmmm... I prefer Michele's answer!

--
Justin C, by the sea.
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      11-03-2006
In article <(E-Mail Removed)>,
Michele Dondi <(E-Mail Removed)> wrote:

> On Fri, 03 Nov 2006 10:17:23 -0000, Justin C
> <(E-Mail Removed)> wrote:
>
> >I was trying to count the number of elements in a hash,

>
> It would be important to know what you mean with "the number of
> elements in a hash": the number of keys()? If so just use that. The
> number of values? Well, that's the same. But if you mean the number of
> *distinct* values, i.e. the cardinality of the image, then you more or
> less fall back on the very FAQ about how to find the distinct elements
> from a *list*.


I did mean number of keys, thank you.

>
> As somebody put it, "the value of a hash in scalar context is of
> interest only to a perl programmer, not to a Perl programmer".


Clear and to the point, nice. Thanks.

--
Justin C, by the sea.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-03-2006
Michele Dondi <(E-Mail Removed)> wrote:

> As somebody put it, "the value of a hash in scalar context is of
> interest only to a perl programmer, not to a Perl programmer".



I lay claim to that one.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-03-2006
Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> Michele Dondi wrote:
>> Justin C wrote:
>>>
>>>print scalar ( %hash ), "\n" ;

>> [snip]
>>>The output, when this is run, is:
>>>4/8

>>
>> As somebody put it, "the value of a hash in scalar context is of
>> interest only to a perl programmer, not to a Perl programmer".

>
> Sometimes I do:
>
> if ( %hash ) {
> # do something...
> }
>
> and I'm certainly not a perl programmer, so "somebody" must be wrong.



"somebody" would instead write:

if ( keys %hash ) {

just to avoid having a maintenance programmer have to go look up
what a hash in scalar context does.




--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      11-03-2006
On 2006-11-03 20:26, Tad McClellan <(E-Mail Removed)> wrote:
> Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>> Michele Dondi wrote:
>>> As somebody put it, "the value of a hash in scalar context is of
>>> interest only to a perl programmer, not to a Perl programmer".

>>
>> Sometimes I do:
>>
>> if ( %hash ) {
>> # do something...
>> }
>>
>> and I'm certainly not a perl programmer, so "somebody" must be wrong.


Note, that you you aren't interested in the value itself, just whether
the value is true.


> "somebody" would instead write:
>
> if ( keys %hash ) {
>
> just to avoid having a maintenance programmer have to go look up
> what a hash in scalar context does.


That also turned out to be quite a bit faster in an application where I
needed to check the (approximate) size of a hash rather frequently.
Getting the number of elements in a hash is fast, counting used buckets
isn't. (or at least wasn't in the version of perl I used then)

hp


--
_ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sysadmin WSR | > ist?
| | | (E-Mail Removed) | Was sonst wäre der Sinn des Erfindens?
__/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-04-2006
Justin C <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> In article <(E-Mail Removed)>,
> (E-Mail Removed)-berlin.de wrote:
>
> > perldoc perldata, look for the paragraph starting
> >
> > If you evaluate a hash in scalar context,

>
> Hmmm... I prefer Michele's answer!


Rightly so. Michele actually discusses the original article and
the questions it raises. That wasn't my intention, I answered the
explicit question "Where does this output come form" with the
appropriate doc pointer. That is more quickly done and requires
less thought, but isn't entirely without merit.

Anno
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-04-2006
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Michele Dondi wrote:
> > Justin C wrote:
> >>
> >>print scalar ( %hash ), "\n" ;

> > [snip]
> >>The output, when this is run, is:
> >>4/8

> >
> > As somebody put it, "the value of a hash in scalar context is of
> > interest only to a perl programmer, not to a Perl programmer".

>
> Sometimes I do:
>
> if ( %hash ) {
> # do something...
> }
>
> and I'm certainly not a perl programmer, so "somebody" must be wrong.


Ah, but it's Perl, so context matters. The scalar value only interests
perl programmers. The boolean value is routinely used by Perl
programmers. Or not, looking at Tad's contribution to this thread.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help in understanding error news ASP .Net 1 10-04-2004 05:06 PM
Understanding error - Java returned: 137 Manish Hatwalne Java 1 09-03-2004 10:01 AM
Re: understanding an error Alvin Andries VHDL 0 09-12-2003 11:38 AM
understanding error message Leon Shaw ASP .Net 7 08-06-2003 02:49 PM
Server Error Understanding Leon Shaw ASP .Net 2 07-28-2003 04:58 PM



Advertisments