Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
散列输出的Perl散列_Perl_Hash - Fatal编程技术网

散列输出的Perl散列

散列输出的Perl散列,perl,hash,Perl,Hash,我在看一个文件。我想要一个散列,它给出一行的第一个数字,作为该行其余部分的所有数字到1的散列的键 我相信我添加的哈希是正确的,因为Dumper打印正确。 但是,打印“$first$secondID\n”不会给我任何输出 while (<FILE>) { chomp $_; if (/(\d+)\t(.+)/) { $firstNum = $1; @seconds = split(/\,/,$2); foreac

我在看一个文件。我想要一个散列,它给出一行的第一个数字,作为该行其余部分的所有数字到1的散列的键

我相信我添加的哈希是正确的,因为Dumper打印正确。 但是,打印“$first$secondID\n”不会给我任何输出

while (<FILE>) {

    chomp $_;

    if (/(\d+)\t(.+)/) {

        $firstNum = $1;
        @seconds  = split(/\,/,$2);

        foreach $following (@seconds) {

            $Pairs->{$firstNum}{$following} = 1;
        }

        foreach $first (sort {$a <=> $b} keys %Pairs) {

            print "$first\n";
            %second = {$Pairs{$first}};

            foreach $secondID (sort {$a <=> $b} keys %second) {

                print "$first $secondID\n";
            }
        }
        print Dumper($Pairs);
    }
    else {

        print "ERROR\n";
    }
}
或者我应该先检查第一把钥匙。然后检查第二个键

if (defined $Pairs{$num1}) {

    $temp = $Pairs{$num1};
    if (defined $temp{$num2}) {

        print "true\n;
    }
}
my%hash;
而(){
我的@numbers=split/\D+/;
我的$key=shift@number;
@{$hash{$key}}{@numbers}=(1)x@numbers;
}
#这样测试一下。。。
如果($hash{$num1}{$num2}){
}
使用:


你有几个错误。首先,您似乎不确定是使用
%Pairs
还是
$Pairs
存储哈希,其次是
%second={$Pairs{$first}}
,它试图将哈希引用分配给哈希
%second
。大概您想要
my%second=%{$Pairs{$first}

您应该始终在所有Perl程序开始时
使用strict
使用warnings
,并在第一次使用时使用
my
声明所有变量。这将提醒您可能容易忽略的简单错误,并显示您在此程序中同时使用了
%Pairs
$Pairs
,以及尝试将单个值(散列引用)分配给散列

与其复制整个散列,不如在
$seconds
中保存对它的引用。然后,您可以在以下
for
循环中取消对它的引用

有经验的Perl程序员也会感谢您对本地(
my
)变量使用小写加下划线,并为包和类名保留大写字母

此程序按预期工作,并希望文件名作为命令行参数:

use strict;
use warnings;

my %pairs;

while (<>) {

  unless ( /(\d+)\s+(.+)/ ) {
    print "ERROR\n";
    next;
  }

  my $first_num = $1;
  my @seconds = split /,/, $2;

  foreach my $following (@seconds) {
    $pairs{$first_num}{$following} = 1;
  }

  foreach my $first (sort { $a <=> $b } keys %pairs) {
    print "$first\n";
    my $second = $pairs{$first};
    foreach my $second_id (sort { $a <=> $b } keys %$second) {
      print "$first $second_id\n";
    }
  }

}
使用严格;
使用警告;
我的%对;
而(){
除非(/(\d+)\s+(.+)/){
打印“错误\n”;
下一个
}
我的$first_num=$1;
我的@seconds=split/,/,$2;
foreach my$following(@秒){
$pairs{$first_num}{$following}=1;
}
foreach my$first(排序{$a$b}键%pairs){
打印“$first\n”;
my$second=$pairs{$first};
foreach my$second_id(排序{$a$b}键%$second){
打印“$first$second\u id\n”;
}
}
}

很抱歉在发布时意外删除。我正在更改变量的名称。问题依然存在。
my %hash;
while ( <> ) {
  my @numbers = split /\D+/;
  my $key     = shift @numbers;
  @{$hash{$key}}{ @numbers } = ( 1 ) x @numbers;
}


# test it this way...
if ( $hash{ $num1 }{ $num2 } ) { 

}
%second = %{$Pairs->{$first}};
use strict;
use warnings;

my %pairs;

while (<>) {

  unless ( /(\d+)\s+(.+)/ ) {
    print "ERROR\n";
    next;
  }

  my $first_num = $1;
  my @seconds = split /,/, $2;

  foreach my $following (@seconds) {
    $pairs{$first_num}{$following} = 1;
  }

  foreach my $first (sort { $a <=> $b } keys %pairs) {
    print "$first\n";
    my $second = $pairs{$first};
    foreach my $second_id (sort { $a <=> $b } keys %$second) {
      print "$first $second_id\n";
    }
  }

}