Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with keys...

Status
Not open for further replies.

jtb1492

Technical User
Mar 17, 2005
25
US
Can sombody explain keys to me? I get that keys %hash returns an array with the key values. But how do I use keys when I have a hash of hashes? How do I find the second set of keys? I'm very new to perl so please be simple in you responses. Thanks, guys.
 
A hash of hashes consists of an outer hash and many inner hashes. To get the keys of the outer hash, you would use:[tt]
@k=keys %outerhash;[/tt]
To get the keys of an inner hash, you would use:[tt]
@k=keys %{$outerhash{'innerhash'}};[/tt]
$outerhash{'innerhash'} represents the value of an element of $outerhash, but that value happens to be a hash, so you can then cast it as a hash by putting it inside %{...} and get its keys.
 
foreach $keylevel2 (keys %{$hash{$keylevel1}}) {
print $hash{$keylevel1}{$keylevel2}, "\n";
}


Michael Libeson
 
I get that keys %hash returns an array with the key values.

It depends on the context you use. If you use:

$num = keys %hash;

you get the number of keys in the hash,

if you use:

@keys = keys %hash;

you get a list of the value of the keys

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top