<menu id="u6gwm"></menu>
<object id="u6gwm"></object>
<input id="u6gwm"></input>
<menu id="u6gwm"><u id="u6gwm"></u></menu>
<input id="u6gwm"><acronym id="u6gwm"></acronym></input>
<menu id="u6gwm"><acronym id="u6gwm"></acronym></menu>
  • <menu id="u6gwm"></menu>
    <input id="u6gwm"><u id="u6gwm"></u></input>
  • 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";
    }
    相关文章
    相关标签/搜索
    4887铁算结果开奖王中王结果小说 东平县| 武义县| 黄龙县| 邹城市| 宁夏| 临清市| 福贡县| 贵南县| 嘉兴市| 云阳县| 察哈| 繁昌县| 监利县| 黎城县| 武隆县| 焉耆| 克东县| 五家渠市| 青龙| 广河县| 怀柔区| 肥东县| 咸阳市| 东港市| 临泉县| 盐山县| 武穴市| 当涂县| 泰州市| 双峰县| 鞍山市| 双柏县| 巢湖市| 台南县| 灵台县| 兴业县| 运城市| http://fa.hz0j1r1vo.fun http://fa.hz0j1r5vo.fun http://fa.hz0j1r0vo.fun http://fa.hz0j2r7vo.fun http://fa.hz0j2r3vo.fun