Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Hash problem: each() failure

by Anonymous Monk
on Feb 22, 2008 at 09:40 UTC ( [id://669498]=perlquestion: print w/replies, xml ) Need Help??

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

I try to display the content in a hash by this code
my %Cls = {'P','a','N','b','U','c'}; print %Cls.": \n\n"; while ( my ($k, $v) = each(%Cls) ) { print "$k => $v\n"; }
But what I got is
1/8: HASH(0x124290) =>
I run in perl 5.8.8, what 's wrong of my code?

Replies are listed 'Best First'.
Re: Hash problem: each() failure
by Corion (Patriarch) on Feb 22, 2008 at 09:43 UTC

    The error is at the top of your code:

    my %Cls = {'P','a','N','b','U','c'};

    You need to assign a list, not a hash reference to your hash:

    my %Cls = ('P','a','N','b','U','c');
Re: Hash problem: each() failure
by hipowls (Curate) on Feb 22, 2008 at 09:55 UTC

    And if warnings had been enabled you would have been told about it.

    Reference found where even-sized list expected at Perl-1.pl line 7.

Re: Hash problem: each() failure
by cdarke (Prior) on Feb 22, 2008 at 14:11 UTC
    Further to what Corion said, you actually created a hash with a single key. The key was a hash reference and its value undef. The braces {...} create an anonymous hash and return a reference to it. You could have done this:
    my $Cls = {'P','a','N','b','U','c'}; print %$Cls,": \n\n"; while ( my ($k, $v) = each(%$Cls) ) { print "$k => $v\n"; }
    Note in the print statement I changed the '.' to a ','. The dot operator forces the hash into scalar context, which is rairly useful.
Re: Hash problem: each() failure
by peter (Sexton) on Feb 22, 2008 at 13:14 UTC
    A stringified hash will show the capacity of the hash. This is (almost) never a useful value.
    Peter Stuifzand
Re: Hash problem: each() failure
by DigitalKitty (Parson) on Feb 23, 2008 at 18:12 UTC
    Hi Anonymous Monk.

    As Corion mentioned, you used the anonymous hash syntax, i.e., $name_of_hash = { ... };

    Assigning a list to a hash variable will result in the key/value pair(s) being created.
    use warnings; use strict; my %Cls = qw( P a N b U c ); print %Cls, "\n\n"; while ( my ($k, $v) = each( %Cls ) ) { print "$k => $v\n"; }

    Update: Fixed typo by changing %name_of_hash = { ... }; to $name_of_hash = { ... }; after ikegami++ noticed it. Thanks.

    Hope this helps,
    ~Katie
Re: Hash problem: each() failure
by Anonymous Monk on Feb 24, 2008 at 17:05 UTC
    To add to the already great comments made...

    When you initialize a hash it is standard convention to use '=>' between key/value pairs rather than commas...

    e.g. {'P' =>'a','N' =>'b','U' =>'c'}

    it is strictly a readability thing, but since code is written for people, readability is everything

    Since a hash is just a "special" array, it may be easier to understand the problem by looking at an array example.

    If you initialiaze an array with square brackets you get a reference to the array. On the other hand, if you initialize with parentheses

    @x = ('P','a','N','b','U','c');

    you will get the actual array.

    Creating the hash with the {} braces works like the square brackets on an array.

    Another great comment made was to suggest you should enable warnings on your code. I cannot agree more. You should just get in the automatic habit of adding strict and warnings to everything you write. e.g.

    #!/usr/bin/perl
    use strict;
    use warnings;

    my 2 bits...

    Anyone know how to get square brackets to show up in my source listing on the perl monks site?
Re: Hash problem: each() failure
by Anonymous Monk on Feb 24, 2008 at 16:46 UTC
    Was ist rather this you wanted?
    my %Cls = ('P' => 'a',
               'N' => 'b',
               'U' => 'c');
    print %Cls.": \n\n";
    foreach $a (keys(%Cls)) {
        print "$a => $Cls{$a}\n";
    }
    
Re: Hash problem: each() failure
by Anonymous Monk on Feb 22, 2008 at 22:26 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-19 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found