简体   繁体   中英

Get the Keys in Hash of Array - Perl

I am trying to write a script where i need to work on Keys of HASH and i am struggling to extract all the keys of HASH. Here is my Code:

use strict;
use warnings;

my %HOA = (
                "age" => [20 ,25],
                "Name" => ["Raj" ,"Kiran"]
            );

foreach my $key ( keys%HOA){
print "Key -> $HOA{$key}\n";
my @array = @{$HOA{$key}};
    foreach my $val (@array){
        print "val -> $val\n";
}
}

I need to store all the keys in an array. Can some on help me out?

This will give you all the array elements from the hash in one array.

use strict;
use warnings;

my %HOA = (
    "age" => [20 ,25],
    "Name" => ["Raj" ,"Kiran"]
    );

my @array=map @{$HOA{$_}},keys %HOA;

foreach my $val (@array)
    {
    print "$val\n";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM