understanding an error

J

Justin C

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.
 
A

anno4000

Justin C said:
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
 
G

Gunnar Hjalmarsson

Michele said:
Justin said:
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. ;-)
 
J

Justin C

Michele Dondi said:
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.
 
T

Tad McClellan

Michele Dondi said:
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.
 
T

Tad McClellan

Gunnar Hjalmarsson said:
Michele said:
Justin said:
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.

:)
 
P

Peter J. Holzer

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
 
A

anno4000

Justin C said:
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
 
A

anno4000

Gunnar Hjalmarsson said:
Michele said:
Justin said:
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
 
D

DJ Stunks

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

by the way, your scalar assignment

here ----------------------------^^^^^^^^

already imposes scalar context rendering it unneccessary to force
context manually

here --------------------------------------^^^^^^

-jp
 
J

Justin C

DJ Stunks said:
by the way, your scalar assignment

here ----------------------------^^^^^^^^

already imposes scalar context rendering it unneccessary to force
context manually

here --------------------------------------^^^^^^

-jp

Thanks for pointing that out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top