Arrays perl中的引用:将数组散列到另一个数组
我在将数组中的哈希引用到另一个数组时遇到问题。 我有一个数组@result,如下所示:Arrays perl中的引用:将数组散列到另一个数组,arrays,perl,hash,reference,Arrays,Perl,Hash,Reference,我在将数组中的哈希引用到另一个数组时遇到问题。 我有一个数组@result,如下所示: @result = ( { "type" => "variable", "s" => "NGDP", "variable" => "NGDP" }, {"type" => "subject", "s" => "USA", "subject" => "USA", "variable" => "NGDP" }, { "type" => "c
@result = (
{ "type" => "variable",
"s" => "NGDP",
"variable" => "NGDP" },
{"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP" },
{ "type" => "colon",
"s" => ",",
"colon" => "," },
{ "type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP" },
{ "type" => "operator",
"s" => "+",
"operator => "+" },
{"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP" },
);
@part_col_all = (
(
{"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP" },
{"type" => "subject",
"s" => "CAN",
"subject" => "CAN",
"variable" => "NGDP" },
),
(
{ "type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP" },
{ "type" => "operator",
"s" => "+",
"operator" => "+" },
{"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP" },
)
);
@input = qw(GDP USA, JPN CHN); # Compare the GDP of the USA with Japan and China together
my $parameter = shift @input; # Remove what you're measuring
my @country_sets; # An array of arrays
my $set = 0 # Which set you're on
for my $value ( @input ) {
if ( $value eq "," ) {
$set += 1; # Next Set
next;
}
push @{ $country_sets[$set] }, $input;
}
@country_sets = (
(
USA,
),
(
JPN,
CHN,
),
)
$country_sets[0] = (
{
"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP",
},
)
$country_sets[1] = (
{
"type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP",
},
{
"type" => "operator",
"s" => "+",
"operator => "+",
},
{
"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP",
},
);
我想将这个数组分成冒号,并将@result数组的元素推送到另一个数组中,所以我编写了脚本:
for ($i = 0; $i <= $#result; $i++) {
if (defined $result[$i]{subject} or $result[$i]{operator} and not defined $result[$i]{colon}) {
push @part_col, \%{$result[$i]};
}
elsif ($i == $#result) {
push @part_col_all, \@part_col;
}
elsif (defined $result[$i]{colon}) {
push @part_col_all, \@part_col;
my @part_col;
}
}
我不知道我做错了什么。对不起,如果有什么基本的错误,我是初学者。非常感谢。首先,您缺少一个报价:
{ "type" => "operator",
"s" => "+",
"operator" => "+" },
^ missing
至于打印,您可以执行以下操作:
foreach my $part (@part_col){
print $part->{operator}."\n";
}
或者在打印周期中使用值执行任何您想执行的操作。您应该阅读以帮助您
取消引用以简化代码没有什么不妥:
my @part_col;
my @part_col_all;
for $i ( 0..$#array ) {
my %hash = ${ $result[$i] }; # Make it easy on yourself. Dereference
if ( defined $hash{subject} or defined $hash{operator} and not defined $hash{colon} ) {
push @part_col, \%hash; # or push, @par_col, $result[$i]
}
}
elsif ($i == $#result) {
push @part_col_all, \@part_col;
}
elsif (defined $hash_ref->{colon}) {
push @part_col_all, \@part_col;
my @part_col;
}
}
请注意,我将的从您必须的三部分设置更改为更干净、更容易理解的陈述方式
仔细查看您的数据结构,我注意到$hash{type}
将告诉您是否定义了$hash{operator}
,$hash{subject}
,或$hash{colon}
。让我们使用$hash{type}
并简化,如果
:
my @part_col;
my @part_col_all;
for my $i ( 0..$#array ) {
my %hash = ${ $result[$i] }; # Make it easy on yourself. Dereference
if ( $hash{type} eq "subject" or $hash{type} eq "operator" ) {
push @part_col, \%hash; # or push, @par_col, $result[$i]
}
}
事实上,既然@array
只是一个数组,我就把它当作一个数组来对待。我将使用一个简单的for
结构遍历数组的每个元素。每个元素都是一个hash_引用,因此:
for my $hash_ref ( @array ) {
my %hash = %{ %hash_ref };
if ( $hash{type} eq "subject" or $hash{type} eq "operator" ) {
push @part_col, \%hash;
}
}
进一步简化,我可以使用->
语法一次取消引用并讨论散列中的特定元素:
for my $hash_ref ( @array ) {
if ( $hash_ref->{type} eq "subject" or $hash_ref->{type} eq "operator" ) {
push @part_col, $hash_ref;
}
}
我正在努力理解您代码的其余部分:
my @part_col;
my @part_col_all;
for $i ( 0..$#array ) {
my %hash = ${ $result[$i] }; # Make it easy on yourself. Dereference
if ( defined $hash{subject} or defined $hash{operator} and not defined $hash{colon} ) {
push @part_col, \%hash; # or push, @par_col, $result[$i]
}
}
elsif ($i == $#result) {
push @part_col_all, \@part_col;
}
elsif (defined $hash_ref->{colon}) {
push @part_col_all, \@part_col;
my @part_col;
}
}
这些将@part\u col
推到@part\u colu\u all
上的动作让我很困惑。您到底想在@part\u col\u all
中存储什么?记住\@part\u col是内存中存储@part\u col
的位置。您将相同的内存位置一次又一次地推送到该散列上,因此您将一次又一次地存储相同的引用。这真的是你想要的吗?我对此表示怀疑
您需要做的是准确地确定您的数据结构真正代表了什么。数据结构应具有可靠的定义。数据结构@part\u col\u all
代表什么?数据结构$part\u col\u all[$i]
代表什么?数据结构$part\u col\u all[$i]->[$j]
代表什么?如果不知道这一点,你很难回答剩下的问题
您是否在一个数组中存储类型为冒号的元素,而在另一个数组中存储所有其他元素?或者您是在一个数组中存储所有内容,还是在另一个数组中存储所有非冒号类型的内容
一旦我明白了这一点,我就可以回答你剩下的问题了
补遗
谢谢你的回复,我会用这种方式写下我的结果。这真的很有帮助。我用关于@part\u col\u all的数据结构的更多信息更新了我的问题。我希望你能理解我想解释的内容,否则我会再试一次
如果我了解你在做什么,有人进入了NGDP USA,JPN+CNA,这意味着你在比较美国与日本和中国的NGDP总和
在我看来,您需要三个独立的变量:
$参数
-您正在测量的内容。(国内生产总值等)
@countries\u set\u 1
-第一组国家
@countries\u set\u 2
-与第一组国家进行比较的第二组国家
你所说的冒号(在美国我们称之为逗号)是第一组国家和第二组国家之间的分隔符。然后,你只需通过一个循环。可能这两个数组只是同一数组的两个元素,而国家集是数组引用。我想象这样的情况:
@result = (
{ "type" => "variable",
"s" => "NGDP",
"variable" => "NGDP" },
{"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP" },
{ "type" => "colon",
"s" => ",",
"colon" => "," },
{ "type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP" },
{ "type" => "operator",
"s" => "+",
"operator => "+" },
{"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP" },
);
@part_col_all = (
(
{"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP" },
{"type" => "subject",
"s" => "CAN",
"subject" => "CAN",
"variable" => "NGDP" },
),
(
{ "type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP" },
{ "type" => "operator",
"s" => "+",
"operator" => "+" },
{"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP" },
)
);
@input = qw(GDP USA, JPN CHN); # Compare the GDP of the USA with Japan and China together
my $parameter = shift @input; # Remove what you're measuring
my @country_sets; # An array of arrays
my $set = 0 # Which set you're on
for my $value ( @input ) {
if ( $value eq "," ) {
$set += 1; # Next Set
next;
}
push @{ $country_sets[$set] }, $input;
}
@country_sets = (
(
USA,
),
(
JPN,
CHN,
),
)
$country_sets[0] = (
{
"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP",
},
)
$country_sets[1] = (
{
"type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP",
},
{
"type" => "operator",
"s" => "+",
"operator => "+",
},
{
"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP",
},
);
这将创建如下数据结构:
@result = (
{ "type" => "variable",
"s" => "NGDP",
"variable" => "NGDP" },
{"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP" },
{ "type" => "colon",
"s" => ",",
"colon" => "," },
{ "type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP" },
{ "type" => "operator",
"s" => "+",
"operator => "+" },
{"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP" },
);
@part_col_all = (
(
{"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP" },
{"type" => "subject",
"s" => "CAN",
"subject" => "CAN",
"variable" => "NGDP" },
),
(
{ "type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP" },
{ "type" => "operator",
"s" => "+",
"operator" => "+" },
{"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP" },
)
);
@input = qw(GDP USA, JPN CHN); # Compare the GDP of the USA with Japan and China together
my $parameter = shift @input; # Remove what you're measuring
my @country_sets; # An array of arrays
my $set = 0 # Which set you're on
for my $value ( @input ) {
if ( $value eq "," ) {
$set += 1; # Next Set
next;
}
push @{ $country_sets[$set] }, $input;
}
@country_sets = (
(
USA,
),
(
JPN,
CHN,
),
)
$country_sets[0] = (
{
"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP",
},
)
$country_sets[1] = (
{
"type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP",
},
{
"type" => "operator",
"s" => "+",
"operator => "+",
},
{
"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP",
},
);
不需要复杂的@results
,因为您只需要对所有相关人员进行一次操作(GDP等)
不过,我想我知道你想要什么了。我们将使用一组数组。以下是我以前的经历:
for my $hash_ref ( @array ) {
if ( $hash_ref->{type} eq "subject" or $hash_ref->{type} eq "operator" ) {
push @part_col, $hash_ref;
}
}
我们将结合上面我提供的代码,将这些国家分为两组:
my @country_sets; # An array of arrays
my $set = 0 # Which set you're on
for my $country_ref ( @array ) {
next if $country_ref->{type} eq "variable"; # We don't want variables
if ( $country_ref{type} eq "colon" ) { # Switch to the other country set
set += 1;
next;
}
push @{ $country_sets[$set] }, $country_ref;
}
前几个条目将进入$country\u set[0]
,这将是一个数组引用。冒号之后(不会输入到集合中),第二组国家将进入$country\u set[1]
,这将是散列引用的另一个数组:
@country\u set
-将输入信息分为两组
@country\u set[$x]
-一组特定的国家(可能还有运营商)
@country\u设置[$x]->[$y]
-特定国家或运营商
@country\u设置[$x]->[$y]->{$key}
-来自特定国家/地区的特定值
其中,$x
从0
变为1
。这将为您提供如下信息:
@result = (
{ "type" => "variable",
"s" => "NGDP",
"variable" => "NGDP" },
{"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP" },
{ "type" => "colon",
"s" => ",",
"colon" => "," },
{ "type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP" },
{ "type" => "operator",
"s" => "+",
"operator => "+" },
{"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP" },
);
@part_col_all = (
(
{"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP" },
{"type" => "subject",
"s" => "CAN",
"subject" => "CAN",
"variable" => "NGDP" },
),
(
{ "type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP" },
{ "type" => "operator",
"s" => "+",
"operator" => "+" },
{"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP" },
)
);
@input = qw(GDP USA, JPN CHN); # Compare the GDP of the USA with Japan and China together
my $parameter = shift @input; # Remove what you're measuring
my @country_sets; # An array of arrays
my $set = 0 # Which set you're on
for my $value ( @input ) {
if ( $value eq "," ) {
$set += 1; # Next Set
next;
}
push @{ $country_sets[$set] }, $input;
}
@country_sets = (
(
USA,
),
(
JPN,
CHN,
),
)
$country_sets[0] = (
{
"type" => "subject",
"s" => "USA",
"subject" => "USA",
"variable" => "NGDP",
},
)
$country_sets[1] = (
{
"type" => "subject",
"s" => "JPN",
"subject" => "JPN",
"variable" => "NGDP",
},
{
"type" => "operator",
"s" => "+",
"operator => "+",
},
{
"type" => "subject",
"s" => "CHN",
"subject" => "CHN",
"variable" => "NGDP",
},
);
谢谢你的回复,我会用这种方式写下我的结果。这真的很有帮助。我用关于@part\u col\u all的数据结构的更多信息更新了我的问题。我希望你能理解我想解释的内容,否则我会再试一次。