<input id="wk422"><u id="wk422"></u></input>
  • <menu id="wk422"><tt id="wk422"></tt></menu><input id="wk422"></input>
    <object id="wk422"></object><input id="wk422"><acronym id="wk422"></acronym></input>
  • <nav id="wk422"></nav>
  • <input id="wk422"><acronym id="wk422"></acronym></input>
  • <object id="wk422"></object>
    <input id="wk422"><u id="wk422"></u></input><input id="wk422"></input>
  • <input id="wk422"><button id="wk422"></button></input>
  • <menu id="wk422"></menu>

    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.hz0j1r3vo.fun http://fa.hz0j2r6vo.fun http://fa.hz0j1r9vo.fun http://fa.hz0j0r4vo.fun http://fa.hz0j1r8vo.fun