How do I loop through anonymous hash?

Discussion in 'Perl Misc' started by Gunnar Hjalmarsson, Feb 13, 2005.

  1. Atlantis wrote:
    >
    > my $hash={Name=>"John Doe", Location=>"New York", Age=>"25"};
    >
    > foreach my $Key (keys ($hash));


    You need to dereference the hash reference to grab the keys, and remove
    that statement separator.

    foreach my $Key (keys %$hash)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 13, 2005
    #1
    1. Advertising

  2. Gunnar Hjalmarsson

    Atlantis Guest

    Hi, I should know this, but can't seem to get it working...

    Basically I've got an anonymous hash that I want to loop through, I've tried
    as below (and a couple of other thing) but get various errors (this one give
    me ~syntax error at hash.pl line, 7, near ");"

    Code Start.
    #!c:\perl\bin\perl
    use strict;
    use warnings;

    my $hash={Name=>"John Doe", Location=>"New York", Age=>"25"};

    foreach my $Key (keys ($hash));
    {

    print "$Key == ", $hash->{$Key}, "\n";
    }
    Code End

    Anyone help me with this?

    Thank you in advance:)
     
    Atlantis, Feb 13, 2005
    #2
    1. Advertising

  3. Gunnar Hjalmarsson

    Peter Scott Guest

    In article <Y3KPd.382$>,
    "Atlantis" <> writes:
    >#!c:\perl\bin\perl
    >use strict;
    >use warnings;
    >
    >my $hash={Name=>"John Doe", Location=>"New York", Age=>"25"};
    >
    >foreach my $Key (keys ($hash));


    keys() needs a hash as argument. You gave it a reference
    to a hash instead. So, dereference $hash to get the hash:

    foreach my $Key (keys %$hash)

    and get rid of the semicolon.

    BTW, it looks weird for a loop variable to be capitalized.

    >{
    >
    > print "$Key == ", $hash->{$Key}, "\n";
    >}


    --
    Peter Scott
    http://www.perldebugged.com/
    *** NEW *** http://www.perlmedic.com/
     
    Peter Scott, Feb 13, 2005
    #3
  4. Atlantis wrote:

    > Hi, I should know this, but can't seem to get it working...
    >
    > Basically I've got an anonymous hash that I want to loop through, I've
    > tried as below (and a couple of other thing) but get various errors (this
    > one give me ~syntax error at hash.pl line, 7, near ");"
    >
    > Code Start.
    > #!c:\perl\bin\perl
    > use strict;
    > use warnings;
    >
    > my $hash={Name=>"John Doe", Location=>"New York", Age=>"25"};
    >
    > foreach my $Key (keys ($hash));


    You haven't dereferenced your reference. Also the semicolon
    doesn't belong here.

    foreach my $Key (keys (%$hash))

    > {
    >
    > print "$Key == ", $hash->{$Key}, "\n";
    > }
    > Code End
    >
    > Anyone help me with this?
    >
    > Thank you in advance:)


    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Feb 13, 2005
    #4
  5. Gunnar Hjalmarsson

    Atlantis Guest

    Thank you to all of you, I can't quite believe I didn't try that (I know I
    tried something like it:$), will have to stop taking those stupid pills.

    Thank you all again:)
     
    Atlantis, Feb 14, 2005
    #5
    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. Reporter
    Replies:
    3
    Views:
    480
    Mike Schilling
    May 12, 2007
  2. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
  3. Replies:
    1
    Views:
    225
  4. Isaac Won
    Replies:
    9
    Views:
    390
    Ulrich Eckhardt
    Mar 4, 2013
  5. Justin C
    Replies:
    1
    Views:
    193
    Justin C
    Oct 7, 2013
Loading...

Share This Page