Perl, convert hash to array

后端 未结 11 2082
借酒劲吻你
借酒劲吻你 2021-02-05 06:56

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 mean

11条回答
  •  失恋的感觉
    2021-02-05 07:42

    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.

提交回复
热议问题