Perl, convert hash to array

2020-05-31 04:51发布

If I have a hash in Perl that contains complete and sequential integer mappings (ie, all keys from from 0 to n are mapped to something, no keys outside of this), is there a means of converting this to an Array?

I know I could iterate over the key/value pairs and place them into a new array, but something tells me there should be a built-in means of doing this.

标签: perl hash arrays
11条回答
Explosion°爆炸
2楼-- · 2020-05-31 05:31
@a = @h{sort { $a <=> $b } keys %h};
查看更多
狗以群分
3楼-- · 2020-05-31 05:32

An easy way is to do @array = %hash

For example,

my %hash = (
    "0"  => "zero",
    "1" => "one",
    "2"  => "two",
    "3"  => "three",
    "4"  => "four",
    "5"  => "five",
    "6"  => "six",
    "7"  => "seven",
    "8"  => "eight",
    "9"  => "nine",
    "10"  => "ten",
);

my @array = %hash;

print "@array"; would produce the following output,

3 three 9 nine 5 five 8 eight 2 two 4 four 1 one 10 ten 7 seven 0 zero 6 six

查看更多
姐就是有狂的资本
4楼-- · 2020-05-31 05:33

This will leave keys not defined in %hashed_keys as undef:

# if we're being nitpicky about when and how much memory
# is allocated for the array (for run-time optimization):
my @keys_arr = (undef) x scalar %hashed_keys;

@keys_arr[(keys %hashed_keys)] =
    @hashed_keys{(keys %hashed_keys)};

And, if you're using references:

@{$keys_arr}[(keys %{$hashed_keys})] = 
    @{$hashed_keys}{(keys %{$hashed_keys})};

Or, more dangerously, as it assumes what you said is true (it may not always be true … Just sayin'!):

@keys_arr = @hashed_keys{(sort {$a <=> $b} keys %hashed_keys)};

But this is sort of beside the point. If they were integer-indexed to begin with, why are they in a hash now?

查看更多
【Aperson】
5楼-- · 2020-05-31 05:34

OK, this is not very "built in" but works. It's also IMHO preferrable to any solution involving "sort" as it's faster.

map { $array[$_] = $hash{$_} } keys %hash; # Or use foreach instead of map

Otherwise, less efficient:

my @array = map { $hash{$_} } sort { $a<=>$b } keys %hash;
查看更多
狗以群分
6楼-- · 2020-05-31 05:35

Perl does not provide a built-in to solve your problem.

If you know that the keys cover a particular range 0..N, you can leverage that fact:

my $n = keys(%hash) - 1;
my @keys_and_values = map { $_ => $hash{$_} } 0 .. $n;
my @just_values     = @hash{0 .. $n};
查看更多
聊天终结者
7楼-- · 2020-05-31 05:37

As DVK said, there is no built in way, but this will do the trick:

my @array = map {$hash{$_}} sort {$a <=> $b} keys %hash;

or this:

my @array;

keys %hash;

while (my ($k, $v) = each %hash) {
    $array[$k] = $v
}

benchmark to see which is faster, my guess would be the second.

查看更多
登录 后发表回答