Perl:取消对数组的引用是否使用标量上下文?
我对Perl和解引用有一个奇怪的问题 我有一个带有数组值的INI文件,在两个不同的部分下,例如Perl:取消对数组的引用是否使用标量上下文?,perl,dereference,Perl,Dereference,我对Perl和解引用有一个奇怪的问题 我有一个带有数组值的INI文件,在两个不同的部分下,例如 [Common] animals =<<EOT dog cat EOT [ACME] animals =<<EOT cayote bird EOT “Common”数组工作正常,即如果$org不是“ACME”,我得到的值是dog cat,但是如果$org等于“ACME”,我得到的值是2 任何想法???取消数组限制当然不是强制使用标量上下文。但使用| |是一种错误。因此,$v
[Common]
animals =<<EOT
dog
cat
EOT
[ACME]
animals =<<EOT
cayote
bird
EOT
“Common”数组工作正常,即如果$org不是“ACME”,我得到的值是dog cat,但是如果$org等于“ACME”,我得到的值是2
任何想法???取消数组限制当然不是强制使用标量上下文。但使用| |是一种错误。因此,$val=$special_val | |默认值;当你的例子不起作用时,你的工作很好 因此@array将包含一个数字,即第一个数组中的元素数,如果是0,则包含第二个数组中的元素数 perlop perldoc页面甚至特别列出了此示例:
In particular, this means that you shouldn't use this for selecting
between two aggregates for assignment:
@a = @b || @c; # this is wrong
@a = scalar(@b) || @c; # really meant this
@a = @b ? @b : @c; # this works fine, though
根据您的需要,解决方案可能是:
my @array = @{$INI{$org}->{animals}}
? @{$INI{$org}->{animals}}
: @{$INI{Common}->{animals}};
解除对数组的限制当然不是强制使用标量上下文。但使用| |是一种错误。因此,$val=$special_val | |默认值;当你的例子不起作用时,你的工作很好 因此@array将包含一个数字,即第一个数组中的元素数,如果是0,则包含第二个数组中的元素数 perlop perldoc页面甚至特别列出了此示例:
In particular, this means that you shouldn't use this for selecting
between two aggregates for assignment:
@a = @b || @c; # this is wrong
@a = scalar(@b) || @c; # really meant this
@a = @b ? @b : @c; # this works fine, though
根据您的需要,解决方案可能是:
my @array = @{$INI{$org}->{animals}}
? @{$INI{$org}->{animals}}
: @{$INI{Common}->{animals}};
谢谢,当场,不知道我在发现此网站之前是如何处理的:。谢谢,当场,不知道我在发现此网站之前是如何处理的:。