将数组存储为映射值问题

时间:2011-03-31 10:20:25

标签: arrays perl hash perl-data-structures

我希望@arr直接包含城市名称。我打印时希望$arr[0]c1。 代码有什么问题?

my $state="Illinois";

 push @{$mstates{$state}}, "c1";

 push @{$mstates{$state}}, "c2";


 my @arr=$mstates{$state};

2 个答案:

答案 0 :(得分:2)

也许你想要

my @arr=@{$mstates{$state}};

答案 1 :(得分:1)

好吧,

my @arr=@{$mstates{$state}}; 

即。,

use strict;
use warnings;
use Data::Dumper;
my $state="Illinois";
my %mstates;
 push @{$mstates{$state}}, "c1";
 push @{$mstates{$state}}, "c2";
 my @arr=@{$mstates{$state}};
 print Dumper(\@arr);

输出:

$VAR1 = [
          'c1',
          'c2'
        ];