You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Perl:如何在同一循环中打印两个哈希数组的子哈希值

解决同一循环中打印哈希数组子哈希值的问题

没问题!我来帮你搞定这个需求。从你给出的哈希结构来看,每个国家都对应两组州数据——旧的(previous)和新的(current),咱们可以通过嵌套循环的方式,把对应的新旧州信息成对输出。

方法一:按索引遍历(假设州顺序一致)

如果每个国家的新旧州数组里,州的顺序是完全对应的(比如第一个都是statea,第二个都是stateb),用索引循环就能直接配对:

use strict;
use warnings;

# 把你的哈希结构转换成可直接使用的变量
my %countries = (
    'countrya' => [ 
        [ { 'area' => 'statea', 'population' => '10' }, { 'area' => 'stateb', 'population' => '20' } ], # previous
        [ { 'area' => 'statea', 'population' => '30' }, { 'area' => 'stateb', 'population' => '40' } ]  # current
    ], 
    'countryb' => [ 
        [ { 'area' => 'statec', 'population' => '50' }, { 'area' => 'stated', 'population' => '60' } ], # previous
        [ { 'area' => 'statec', 'population' => '70' }, { 'area' => 'stated', 'population' => '80' } ]  # current
    ]
);

# 遍历每个国家
foreach my $country (keys %countries) {
    print "=== $country ===\n";
    
    # 取出旧数据和新数据的州数组
    my ($prev_states, $curr_states) = @{$countries{$country}};
    
    # 按索引同时遍历新旧州数据
    for my $i (0 .. $#$prev_states) {
        my $prev_state = $prev_states->[$i];
        my $curr_state = $curr_states->[$i];
        
        # 可选:检查州名是否匹配,避免顺序出错
        if ($prev_state->{area} eq $curr_state->{area}) {
            printf "State: %s\n  Previous Population: %s\n  Current Population: %s\n",
                $prev_state->{area},
                $prev_state->{population},
                $curr_state->{population};
        } else {
            warn "⚠️ $country中出现州名不匹配:$prev_state->{area} vs $curr_state->{area}";
        }
    }
    print "\n";
}

运行这段代码后,输出结果会是:

=== countrya ===
State: statea
  Previous Population: 10
  Current Population: 30
State: stateb
  Previous Population: 20
  Current Population: 40

=== countryb ===
State: statec
  Previous Population: 50
  Current Population: 70
State: stated
  Previous Population: 60
  Current Population: 80

方法二:按州名匹配(更灵活,不受顺序影响)

如果你的新旧州数组里,州的顺序可能不一致,或者新增/删除了某些州,那可以先把旧数据转成以州名为键的哈希,再通过州名匹配新旧数据:

use strict;
use warnings;

my %countries = (
    # 同上面的哈希结构,这里省略重复代码
);

foreach my $country (keys %countries) {
    print "=== $country ===\n";
    
    my ($prev_states, $curr_states) = @{$countries{$country}};
    
    # 将旧州数据转换成{州名 => 人口}的哈希
    my %prev_pop_map = map { $_->{area} => $_->{population} } @$prev_states;
    
    # 遍历新州数据,匹配对应的旧数据
    foreach my $curr_state (@$curr_states) {
        my $state_name = $curr_state->{area};
        if (exists $prev_pop_map{$state_name}) {
            printf "State: %s\n  Previous Population: %s\n  Current Population: %s\n",
                $state_name, $prev_pop_map{$state_name}, $curr_state->{population};
        } else {
            warn "⚠️ $country中找不到$state_name的旧数据";
        }
    }
    print "\n";
}

这种方法不管州的顺序如何,都能精准匹配到对应的新旧人口数据,容错性更强。

内容的提问来源于stack exchange,提问作者Jon J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:37:12