Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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,我在访问散列中的变量时遇到问题,我不知道我做错了什么。在调试哈希%list1的值时,给出了一个undef,因此我无法获取我的值 use strict ; use warnings ; my $text = "The, big, fat, little, bastards"; my $Author = "Alex , Shuman ,Directory"; my %hashes = {1,2,3,40}; my %count = (); my @lst = split(",",$text);

我在访问散列中的变量时遇到问题,我不知道我做错了什么。在调试哈希%list1的值时,给出了一个undef,因此我无法获取我的值

use strict ;
use warnings ;
my $text = "The, big, fat, little, bastards";
my $Author = "Alex , Shuman ,Directory";
my %hashes  = {1,2,3,40};
my %count  = ();
my @lst = split(",",$text);
my $i = 0 ;
my @Authors  =  split(",", $Author);
foreach  my $SingleAuthor(@Authors)
{
    foreach my $dig (@lst)
    {

     $count{$SingleAuthor}{$dig}++;
    }
}

counter(\%count);

sub counter
{
    my $ref  =  shift;
    my @SingleAuthors = keys %$ref;
    my %list1;
    foreach  my $SingleAuthor1(@SingleAuthors)
  {   
   %list1 =  $ref->{$SingleAuthor1};
    foreach my $dig1 (keys %list1)
    {

     print  $ref->{$SingleAuthor1}->{$dig1};
    }
}


}

在两个地方,您试图将哈希引用分配给哈希,这会导致以下警告:在预期大小为偶数的列表中找到引用

以下是您需要进行的两次编辑:

# I changed {} to ().
# However, you never use %hashes, so I'm not sure why it's here at all.
my %hashes  = (1,2,3,40);

# I added %{} around the right-hand side.
%list1 =  %{$ref->{$SingleAuthor1}};
有关复杂数据结构的有用简要讨论,请参见

值得一提的是,通过删除中间变量,可以简化计数器方法而不损失可读性

sub counter {
    my $tallies = shift;
    foreach my $author (keys %$tallies) {   
        foreach my $dig (keys %{$tallies->{$author}}) {
            print $tallies->{$author}{$dig}, "\n";
        }
    }
}
或者,正如伊斯特所指出的,如果你不需要钥匙,就像这样:

    foreach my $author_digs (values %$tallies) {   
        print $dig, "\n" for values %$author_digs;
    }

在两个地方,您试图将哈希引用分配给哈希,这会导致以下警告:在预期大小为偶数的列表中找到引用

以下是您需要进行的两次编辑:

# I changed {} to ().
# However, you never use %hashes, so I'm not sure why it's here at all.
my %hashes  = (1,2,3,40);

# I added %{} around the right-hand side.
%list1 =  %{$ref->{$SingleAuthor1}};
有关复杂数据结构的有用简要讨论,请参见

值得一提的是,通过删除中间变量,可以简化计数器方法而不损失可读性

sub counter {
    my $tallies = shift;
    foreach my $author (keys %$tallies) {   
        foreach my $dig (keys %{$tallies->{$author}}) {
            print $tallies->{$author}{$dig}, "\n";
        }
    }
}
或者,正如伊斯特所指出的,如果你不需要钥匙,就像这样:

    foreach my $author_digs (values %$tallies) {   
        print $dig, "\n" for values %$author_digs;
    }

当我运行您的代码时,Perl准确地告诉了我问题所在

$ ./hash
Reference found where even-sized list expected at ./hash line 7.
Reference found where even-sized list expected at ./hash line 30.
Use of uninitialized value in print at ./hash line 34.
Reference found where even-sized list expected at ./hash line 30.
Use of uninitialized value in print at ./hash line 34.
Reference found where even-sized list expected at ./hash line 30.
Use of uninitialized value in print at ./hash line 34.
如果不清楚,您可以打开诊断以获得更详细的描述

$ perl -Mdiagnostics hash
Reference found where even-sized list expected at hash line 7 (#1)
    (W misc) You gave a single reference where Perl was expecting a list
    with an even number of elements (for assignment to a hash). This usually
    means that you used the anon hash constructor when you meant to use
    parens. In any case, a hash requires key/value pairs.

        %hash = { one => 1, two => 2, };    # WRONG
        %hash = [ qw/ an anon array / ];    # WRONG
        %hash = ( one => 1, two => 2, );    # right
        %hash = qw( one 1 two 2 );          # also fine

当我运行您的代码时,Perl准确地告诉了我问题所在

$ ./hash
Reference found where even-sized list expected at ./hash line 7.
Reference found where even-sized list expected at ./hash line 30.
Use of uninitialized value in print at ./hash line 34.
Reference found where even-sized list expected at ./hash line 30.
Use of uninitialized value in print at ./hash line 34.
Reference found where even-sized list expected at ./hash line 30.
Use of uninitialized value in print at ./hash line 34.
如果不清楚,您可以打开诊断以获得更详细的描述

$ perl -Mdiagnostics hash
Reference found where even-sized list expected at hash line 7 (#1)
    (W misc) You gave a single reference where Perl was expecting a list
    with an even number of elements (for assignment to a hash). This usually
    means that you used the anon hash constructor when you meant to use
    parens. In any case, a hash requires key/value pairs.

        %hash = { one => 1, two => 2, };    # WRONG
        %hash = [ qw/ an anon array / ];    # WRONG
        %hash = ( one => 1, two => 2, );    # right
        %hash = qw( one 1 two 2 );          # also fine

如果$author和$dig确实仅用作键,则可以通过循环使用标题中的值来简化它。如果$author和$dig确实仅用作键,则可以通过循环使用值来简化它