Perl, convert hash to array

后端 未结 11 2083
借酒劲吻你
借酒劲吻你 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:21

    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

提交回复
热议问题