处理缺少的参考 [英] Handling missing references

查看:90
本文介绍了处理缺少的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Perl脚本中收到错误不能使用未定义的值作为ARRAY引用。下面是一个高度简化的版本。

基本上我已经设置了一些数组的散列,其中一些可能是空的(在这种情况下为B)。它工作得很好,如果我不排序数据数组。我试过添加一个条件来测试这个特定的数组是否存在,但它不喜欢这个设置。

 使用strict;它会被填充(不像这个例子中的那样)。 
使用警告;

my @list =('A','B','C');
my%data_for;
$ data_for {'A'} = ['apple','astronaut','acorn'];
$ data_for {'C'} = ['car','cook','candy'];

#创建错误
foreach我的$信(@list){
print$ letter:;
foreach my $ item(sort @ {$ data_for {$ letter}}){
print$ item,;
}
打印\\\
;



$ b $ p
$ b这是我想要的输出(看起来很明显,但是呃): p>

  A:橡子,苹果,宇航员,
B:
C:糖果,汽车,厨师,

奇怪的是,如果我先打印工作版本(不包括排序),则第二个版本排序工作没有错误。我不明白这一点,但我可以将它作为解决方案。

$ data_for {B}
没有定义,所以试图将它解引用为一个数组(在 sort @ {$ data_for {$ letter}}中一个解决方法是为 $ data_for {B} :

  $ data_for {'A'} = ['apple','astronaut','acorn']; 
$ data_for {'B'} = [];
$ data_for {'C'} = ['car','cook','candy'];

但更常用的解决方法是使用'||'运算符(或'//'运算符对于Perl> = v5.10),当没有定义散列值时指定默认值

  foreach my $ item @ {$ data_for {$ letter} || []}){...} 

code> $ data_for {$ letter} 未定义(因此计算结果为false),表达式 $ data_for {$ letter} || [] 会计算一个空数组的引用,并且数组解引用操作将成功。


I'm getting the error Can't use an undefined value as an ARRAY reference in a Perl script. Below is a highly simplified version.

Basically I have setup a hash of arrays where some of them might be empty (B in this case). It works just fine if I DON'T sort the data array. I've tried adding a conditional to test if that particular array exists, but it doesn't like that in this setup. It would not be easy to sort the array as it's populated (unlike in this example).

use strict;
use warnings;

my @list = ('A', 'B', 'C');
my %data_for;
$data_for{'A'} = ['apple', 'astronaut', 'acorn'];
$data_for{'C'} = ['car', 'cook', 'candy'];

# Creates error
foreach my $letter (@list) {
    print "$letter: ";
    foreach my $item ( sort @{$data_for{$letter}}) {
        print "$item, ";
    }
    print "\n";
}

This is the output I want (seems obvious, but eh):

A: acorn, apple, astronaut, 
B:
C: candy, car, cook,

As a strange aside, if I print the working version (without the sort) first, the second version with the sort works without an error. I do not understand this, but I may be able to use that as a work around.

解决方案

$data_for{B} is not defined, so the attempt to dereference it as an array (in the sort @{$data_for{$letter}} expression) is an error.

One workaround is to assign a (empty) value for $data_for{B}:

$data_for{'A'} = ['apple', 'astronaut', 'acorn'];
$data_for{'B'} = [];
$data_for{'C'} = ['car', 'cook', 'candy'];

but a more general workaround is to use the '||' operator (or '//' operator for Perl >=v5.10) to specify a default value when a hash value is not defined

foreach my $item ( sort @{$data_for{$letter} || []}) { ... }

When $data_for{$letter} is undefined (and therefore evaluates to false), the expression $data_for{$letter} || [] evaluates to a reference to an empty array, and the array dereference operation will succeed.

这篇关于处理缺少的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆