Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How do I loop through anonymous hash?

Reply
Thread Tools

How do I loop through anonymous hash?

 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-13-2005
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
 
Reply With Quote
 
 
 
 
Atlantis
Guest
Posts: n/a
 
      02-13-2005
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


 
Reply With Quote
 
 
 
 
Peter Scott
Guest
Posts: n/a
 
      02-13-2005
In article <Y3KPd.382$(E-Mail Removed)>,
"Atlantis" <(E-Mail Removed)> 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/
 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      02-13-2005
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?"
 
Reply With Quote
 
Atlantis
Guest
Posts: n/a
 
      02-14-2005
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


 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
Is this a local anonymous class or a member anonymous class Reporter Java 3 05-12-2007 05:23 AM
help with an anonymous array of anonymous hashes noeldamonmiller@gmail.com Perl Misc 1 02-10-2005 01:08 AM



Advertisments