<address id="ll1nf"><listing id="ll1nf"><mark id="ll1nf"></mark></listing></address>

    <address id="ll1nf"></address>

        <sub id="ll1nf"></sub>

        <thead id="ll1nf"><var id="ll1nf"><output id="ll1nf"></output></var></thead>

        <address id="ll1nf"><dfn id="ll1nf"></dfn></address><form id="ll1nf"><listing id="ll1nf"><mark id="ll1nf"></mark></listing></form>

          <address id="ll1nf"><dfn id="ll1nf"></dfn></address>

          perl – 在脚本的不同部分使用不同的哈希值

          我有一个从文件中收集的哈希.下面的脚本将按预期打印所有值(注意我只包含脚本的重要部分,因此打开输入文件时不会添加太宽):

          use strict;
          use warnings;
          
          my %hash = ();
          while (<$fh>) {
            my ($region, $site, $layer) = split;
            push @{$hash{$region}}, "$region $site $layer";
          }
          foreach my $values(keys %hash) {
            print $values,"\n";
          }

          然而我的问题是我不想根据键打印所有值,我实际上想要通过告诉perl我想要的东西来分割值并在不同区域打印它们,而不仅仅是打印$values.

          例如,如果我有以下数据:

          North belair layer2
          North belair layer3
          East Monte layer2
          East Vista layer3

          我实际上希望能够在其他区域使用这些值,并打印如下内容:

          print "$site has layers: $layer"

          将打印:

          belair has layers: layer2 layer3

          Monte has layers: layer2

          Vista has layers: layer3

          要么

          print "$region has sites, $site and $site has layers: $layer";

          然后将打印:

          North has sites: belair and layers: layer2 layer3

          East has sites: Monte Vista and layers: layer2 layer3

          当我运行多个foreach循环时,我得到重复的结果,我不知道转到哪个方向.我所做的每一次搜索都只显示了如何打印键的所有$值

          my %sites;
          my %regions;
          while (<>) {
             my ($region_id, $site_id, $layer_id) = split;
          
             # Use hashes instead of arrays to remove duplicates.    
             ++$sites{$site_id}{layers}{$layer_id};
             ++$regions{$region_id}{layers}{$layer_id};
             ++$regions{$region_id}{sites}{$site_id};
          }
          
          # "Flatten" hashes into sorted arrays of unique elements.
          for my $site_id (keys(%sites)) {
             my $site = $sites{$site_id};
             $site->{layers} = [ sort keys(%{ $site->{layers} }) ];
          }
          
          for my $region_id (keys(%regions)) {
             my $region = $regions{$region_id};
             $region->{layers} = [ sort keys(%{ $region->{layers} }) ];
             $region->{sites}  = [ sort keys(%{ $region->{sites}  }) ];
          }
          
          # Now do what we want to do.
          for my $site_id (sort keys(%sites)) {
             my $site = $sites{$site_id};
             print "$site_id has layers @{ $site->{layers} }\n";
          }
          
          for my $region_id (sort keys(%regions)) {
             my $region = $regions{$region_id};
             print "$region_id has sites @{ $region->{sites} } and layers @{ $region->{layers} }\n";
          }
          相关文章
          相关标签/搜索
          今晚一肖一码 遵义县| 上思县| 泰州市| 界首市| 金川县| 错那县| 泰宁县| 正阳县| 广汉市| 大关县| 田阳县| 黄冈市| 东山县| 侯马市| 延川县| 壶关县| 潜江市| 马关县| 浙江省| 溆浦县| 琼结县| 游戏| 墨江| 普兰县| 盐亭县| 中方县| 东明县| 寿宁县| 阳城县| 高陵县| 昌乐县| 黄陵县| 鹰潭市| 甘谷县| 当涂县| 二手房| 丹阳市| http://fa.hz0j2r9vo.fun http://fa.hz0j1r9vo.fun http://fa.hz0j2r3vo.fun http://fa.hz0j0r7vo.fun http://fa.hz0j1r3vo.fun