Arrays Perl-从散列引用中存储的数组获取数组值的问题
因此,我目前正在从事一个perl项目,其中我需要将存储为哈希引用的数组(包含要拒绝的Id)传递给另一个子系统,在该子系统中,我使用正在创建的json文件中的内容访问该数组。我目前希望将数组作为ref存储在我的主散列中;“$self”,我在潜艇之间扔下它,以便访问我用来获得授权的令牌。对于我正在工作的站点:Arrays Perl-从散列引用中存储的数组获取数组值的问题,arrays,perl,hashref,Arrays,Perl,Hashref,因此,我目前正在从事一个perl项目,其中我需要将存储为哈希引用的数组(包含要拒绝的Id)传递给另一个子系统,在该子系统中,我使用正在创建的json文件中的内容访问该数组。我目前希望将数组作为ref存储在我的主散列中;“$self”,我在潜艇之间扔下它,以便访问我用来获得授权的令牌。对于我正在工作的站点: #!/usr/bin/perl -I/usr/local/lib/perl #all appropriate libaries used #I use this to gen. my gen
#!/usr/bin/perl -I/usr/local/lib/perl
#all appropriate libaries used
#I use this to gen. my generic user agent, which is passed around.
my $hub = website->new( email=>'XXXX', password=>'XXXX');
# I use this to get the authorisation token, which is stored as a hash ref for accessing where needed (sub not shown)
my $token = $hub->auth();
#this is where I get the array of ids I want to reject
my @reject_array = $hub->approvelist();
# this is where the issue is happening as the array hash ref keeps just giving a total number of id's
$hub->rejectcontacts;
sub approvelist{
my $self = shift;
#$self useragent and the token gets used in a http request to get json
.
#json code here to strip out the id for each contact in a for loop, which I then push into the array below \/
.
.
.
push @idlist, $id;
.
.
.
#this is where I try to store my array as a hash
$self->{reject_array} = @idlist;
ref in $self
return @idlist;
}
sub rejectcontacts{
#I do the same trick to try getting the useragent obj, the auth. token AND now the array
my $self = shift;
.
#code used for the HTTP request here...
.
.
my $content = encode_json({
reason=>"Prospect title",
itemIds=>[@{$self->{reject_array}}], #this is where the problem is
});
问题是,当我试图从哈希引用访问数组时,我似乎只能得到数组中元素数量的标量数,而不是数组中的每个实际内容
我已经检查了数组的定义和hash-ref本身的定义,但是当我在上面的“rejectcontacts”子部分中尝试访问数组时,我得到的只是其中的id数-68(68这个数字被放入我试图编写的json文件中,然后显然不起作用)
如果您能在这方面提供帮助,我将不胜感激-我还没有在其他地方找到解决方案。这里:
$self->{reject_array} = @idlist;
将数组指定给标量。这会导致数组的长度被指定。在尝试取消对标量的引用之前,问题不会出现,如下所示:
itemIds => [@{$self->{reject_array}}]
您可能想要:
$self->{reject_array} = \@idlist;
还请注意:
itemIds => [@{$self->{reject_array}}]
可以简化为:
itemIds => $self->{reject_array}
唯一的区别是,这不会复制数组,但就您的代码而言,这并没有什么不同。这只会提高效率
参考资料:
如果在标量上下文中计算数组,它将返回数组的长度
在这里:
将数组指定给标量。这会导致数组的长度被指定。在尝试取消对标量的引用之前,问题不会出现,如下所示:
itemIds => [@{$self->{reject_array}}]
您可能想要:
$self->{reject_array} = \@idlist;
还请注意:
itemIds => [@{$self->{reject_array}}]
可以简化为:
itemIds => $self->{reject_array}
唯一的区别是,这不会复制数组,但就您的代码而言,这并没有什么不同。这只会提高效率
参考资料:
如果在标量上下文中计算数组,它将返回数组的长度
悬念要了我的命悬念要了我的命。