Gossamer Forum
Home : General : Perl Programming :

Hash Loop

Quote Reply
Hash Loop
Hi,

I have a hash like:

KEY => { SUBKEY => VALUE, SUBKEY => VALUE }

How do I loop through the SUBKEY's ?

foreach %{$hash{KEY}} doesn't seem to work. Nor does %$hash{KEY}


Last edited by:

RedRum: Nov 13, 2001, 8:18 PM
Quote Reply
Re: [RedRum] Hash Loop In reply to
Code:
foreach $subkey (keys(%key)) {
$value = $key{$subkey};
}

- Mark

Astro-Boy!!
http://www.zip.com.au/~astroboy/

Last edited by:

AstroBoy: Nov 13, 2001, 9:03 PM
Quote Reply
Re: [AstroBoy] Hash Loop In reply to
Yes I know how to loop through a normal hash but that isn't what I need.

Thanks anyway.
Quote Reply
Re: [AstroBoy] Hash Loop In reply to
Don't worry...I found the solution. I needed:

Code:
foreach (keys %{$attr{KEY}}) {
$query .= $_ . '=' . $dbh->quote($attr{KEY}{$_}) . $bool;
}

Last edited by:

RedRum: Nov 14, 2001, 5:15 AM
Quote Reply
Re: [RedRum] Hash Loop In reply to
Where did you find that?

- wil
Quote Reply
Re: [Wil] Hash Loop In reply to
In my head. I just used trial and error and managed to get it working.
Quote Reply
Re: [PaulW] Hash Loop In reply to
In Reply To:
Yes I know how to loop through a normal hash but that isn't what I need.

Thanks anyway.
You wanted to loop through the hash and get all the subkeys didnt you? That's how I read your first post anyway...

Oh well, glad you found a solution Cool

- Mark

Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: [AstroBoy] Hash Loop In reply to
Nope I wanted to loop through the SUBKEYS, the code you provided only looped through the main KEY and would have printed a hash reference if I'd have printed $value.

Last edited by:

PaulW: Nov 14, 2001, 5:58 PM
Quote Reply
Re: [PaulW] Hash Loop In reply to
Yep, I getcha now. Read the first post wrong, D'OH! Angelic

Wink
- Mark

Astro-Boy!!
http://www.zip.com.au/~astroboy/