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,我有一个Perl类,它包含一个用于存储其他对象的哈希实例变量。我想有一个方法来打印散列中的元素数,但是我在returnkey($self->{u things})行上得到了以下错误消息 参数1到键的类型必须是散列(不是散列元素) 使用 $self->{u things'}只是一个对散列的引用,keys()需要一个散列作为其参数,因此,您必须首先通过将其包装在%{…}中来解除对它的引用。最后,由于要对项目进行计数,因此必须确保keys()(这是一个列表)的返回值在标量上下文中进行解释,方法是将其包

我有一个Perl类,它包含一个用于存储其他对象的哈希实例变量。我想有一个方法来打印散列中的元素数,但是我在
returnkey($self->{u things})行上得到了以下错误消息

参数1到键的类型必须是散列(不是散列元素)

使用

$self->{u things'}
只是一个对散列的引用,
keys()
需要一个散列作为其参数,因此,您必须首先通过将其包装在
%{…}
中来解除对它的引用。最后,由于要对项目进行计数,因此必须确保
keys()
(这是一个列表)的返回值在标量上下文中进行解释,方法是将其包装在
标量(…)
中使用


$self->{u things'}
只是一个对散列的引用,
keys()
需要一个散列作为其参数,因此,您必须首先通过将其包装在
%{…}
中来解除对它的引用。最后,由于要对项目进行计数,您必须确保
keys()
(这是一个列表)的返回值在标量上下文中进行了解释,方法是将其包装在
标量(…)
中,错误消息完全正确!;)您需要将元素“解引用”到散列中,还需要在标量上下文中调用keys()以获取计数

$ perl -wle 'use strict; my $href = { foo => undef }; $href->{foo} = (); print sclar keys $href->{foo}'
Type of arg 1 to keys must be hash (not hash element) at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
vs

您可能希望对$self->{u things}使用hash ref,以避免意外的列表扁平化和其他问题

$ perl -wle 'use strict; my $href = { foo => undef };  $href->{foo} = { bar => 1 }; print scalar keys %{ $href->{foo} };'
1

错误消息完全正确!;)您需要将元素“解引用”到散列中,还需要在标量上下文中调用keys()以获取计数

$ perl -wle 'use strict; my $href = { foo => undef }; $href->{foo} = (); print sclar keys $href->{foo}'
Type of arg 1 to keys must be hash (not hash element) at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
vs

您可能希望对$self->{u things}使用hash ref,以避免意外的列表扁平化和其他问题

$ perl -wle 'use strict; my $href = { foo => undef };  $href->{foo} = { bar => 1 }; print scalar keys %{ $href->{foo} };'
1

如果我理解正确,
$self->{u things}
应该包含一个哈希数据结构。如果是,您有两个问题:

sub new {
    my ($class) = @_;
    my $self = {
        # Initialize _things to be a reference to an empty hash.
        _things => {},
    };
    bless $self, $class;
    return $self;
}

sub get_count {
    my ( $self ) = @_;
    # Here's the way to get the N of keys.
    # The %{ FOO } syntax will take a hash reference (FOO in this case) and 
    # convert it to a hash, on which we can then call keys().
    return scalar keys %{ $self->{'_things'} };
}

如果我理解正确,
$self->{u things}
应该包含一个哈希数据结构。如果是,您有两个问题:

sub new {
    my ($class) = @_;
    my $self = {
        # Initialize _things to be a reference to an empty hash.
        _things => {},
    };
    bless $self, $class;
    return $self;
}

sub get_count {
    my ( $self ) = @_;
    # Here's the way to get the N of keys.
    # The %{ FOO } syntax will take a hash reference (FOO in this case) and 
    # convert it to a hash, on which we can then call keys().
    return scalar keys %{ $self->{'_things'} };
}