【发布时间】:2014-07-17 08:11:27
【问题描述】:
问题在代码中解释为 cmets:
我有以下一段代码,我试图在其中创建一个散列,其中键本身是对其他数组的引用。
my @arr1=(1,2,3,4,5,6,7,8,9,10);
my @arr2=(1001,1002,1003);
$FILES_1=\@arr1;
$num1=2;
$FILES_2=\@arr2;
$num2=4;
@FILES=($FILES_1, $FILES_2);
@NUMS=($num1,$num2);
fun (\@FILES,\@NUMS);
sub fun{
my ($rFILES,$rNUMS) = @_;
print "\n --${$rFILES}[0]->[2] \n "; # This is same as below
print "\n --$rFILES->[0]->[2] \n "; # This is same as above
my %hash=();
$hash{$rFILES->[0]} = $rNUMS->[0];
my $test = $rFILES->[0];
print "\nTEST : $test->[1]";
my @key = keys %hash;
print "\nKey 1 = $key[0]"; # This prints scalar value as below
print "\ntest = $test "; # This prints scalar value as above
print "\nKey 1->[1] = ${$key[0]}->[1]"; #PROBLEM : THIS DOESNT PRINT SAME AS BELOW
print "\ntest->[1] = $test->[1] "; #THIS PRINTS AS EXPECTED.
}
输出:
--3
--3
TEST : 2
Key 1 = ARRAY(0x1bbb540)
test = ARRAY(0x1bbb540)
Key 1->[1] =
test->[1] = 2
我们不应该保留哈希的键作为对某个数组的引用吗?为什么不打印值“2”?
【问题讨论】:
-
use strict;然后use warnings;
标签: perl