understanding an error

Discussion in 'Perl Misc' started by Justin C, Nov 3, 2006.

  1. Justin C

    Justin C Guest

    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.
    Justin C, Nov 3, 2006
    #1
    1. Advertising

  2. Justin C

    -berlin.de Guest

    Justin C <> 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
    -berlin.de, Nov 3, 2006
    #2
    1. Advertising

  3. 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
    Gunnar Hjalmarsson, Nov 3, 2006
    #3
  4. Justin C

    Justin C Guest

    In article <>,
    -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.
    Justin C, Nov 3, 2006
    #4
  5. Justin C

    Justin C Guest

    In article <>,
    Michele Dondi <> wrote:

    > On Fri, 03 Nov 2006 10:17:23 -0000, Justin C
    > <> 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.
    Justin C, Nov 3, 2006
    #5
  6. 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".



    I lay claim to that one.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 3, 2006
    #6
  7. Gunnar Hjalmarsson <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 3, 2006
    #7
  8. On 2006-11-03 20:26, Tad McClellan <> wrote:
    > Gunnar Hjalmarsson <> 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?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
    Peter J. Holzer, Nov 3, 2006
    #8
  9. Justin C

    -berlin.de Guest

    Justin C <> wrote in comp.lang.perl.misc:
    > In article <>,
    > -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
    -berlin.de, Nov 4, 2006
    #9
  10. Justin C

    -berlin.de Guest

    Gunnar Hjalmarsson <> 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
    -berlin.de, Nov 4, 2006
    #10
  11. Justin C

    DJ Stunks Guest

    Justin C wrote:
    > 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
    DJ Stunks, Nov 4, 2006
    #11
  12. Justin C

    Justin C Guest

    In article <>,
    "DJ Stunks" <> wrote:

    > Justin C wrote:
    > > 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


    Thanks for pointing that out.

    --
    Justin C, by the sea.
    Justin C, Nov 4, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin Andries

    Re: understanding an error

    Alvin Andries, Sep 12, 2003, in forum: VHDL
    Replies:
    0
    Views:
    472
    Alvin Andries
    Sep 12, 2003
  2. Leon Shaw

    Server Error Understanding

    Leon Shaw, Jul 28, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    431
    John Saunders
    Jul 28, 2003
  3. Leon Shaw

    understanding error message

    Leon Shaw, Aug 5, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    372
    S. Justin Gengo
    Aug 6, 2003
  4. news
    Replies:
    1
    Views:
    373
    bruce barker
    Oct 4, 2004
  5. Manish Hatwalne

    Understanding error - Java returned: 137

    Manish Hatwalne, Sep 3, 2004, in forum: Java
    Replies:
    1
    Views:
    7,287
    Thomas Fritsch
    Sep 3, 2004
Loading...

Share This Page