我想讓 @{$allHash{$key1}}成為 ["item1", "item2"]。
推送不起作用,所有的列印陳述句都是為了試圖找到被推送的專案的去向。 在推送中使用->符號更糟糕,它使$temp[0][0]和第24行顯示一個陣列而不是專案。
#use strict; # I've turned these off here to make the code easier to read
#use warnings;/span>
my %allHash = ();
my $key1 = "key1"/span>;
my $item1 = " item1";
my $item2 = "item2";
#if (!existence($allHash{key1})) {$allHash{key1}=();}; # 沒有區別,反正陣列會自動生成。
push (@{$allHash{$key1}}, $item1); # push 1st item.
print"
at11: pushed $key1, $item1"。
my @temp = $allHash{$key1};
print"
at13:temp=@temp, length=",0 @temp, ", temp[0] =$temp[0], temp[0][0]=$temp[0][0]";
print"
at14: allHash{$key1}[0]=$allHash{$key1}[0]"。
print"
at15: allHash{$key1}[1]=$allHash{$key1}[1]"。
print"
at16: allHash{$key1}[0][0]=$allHash{$key1}[0][0]"。
print"
at17: allHash{$key1}[1][0]=$allHash{$key1}[1][0]"。
print"
at18: allHash{$key1}[0][1]=$allHash{$key1}[0][1] 。
"。
print"
----------------";
push (@{$allHash{$key1}}, $item2); # push 2d item.
print"
at21: pushed $key1, $item2"。
@temp = @{allHash{$key1}};
print"
at23:temp=@temp, length=",0 @temp, ", temp[0]=$temp[0], temp[0][0] =$temp[0][0]"。
print"
at24: allHash{$key1}[0]=$allHash{$key1}[0]"。
print"
at25: allHash{$key1}[1]=$allHash{$key1}[1], allHash{$key1}[1][0] =$allHash{$key1}[1][0]"/span>。
print"
at26: allHash{$key1}[0][0]=$allHash{$key1}[0][0]"。
print"
at27: allHash{$key1}[1][0]=$allHash{$key1}[1][0]"。
print"
at28: allHash{$key1}[0][1]=$allHash{$key1}[0][1] 。
"。
上述程式的輸出是:
at11: pushed key1, item1
at13: temp=, length=1, temp[0]=ARRAY(0x331eb8), temp[0] [0]=item1
at14: allHash{key1}[0]=item1
at15: allHash{key1}[1]=
at16: allHash{key1}[0] [0]=
at17: allHash{key1}[1][0]=
at18: allHash{key1}[0][1]=
----------------
at21: 推送key1, item2
at23: temp=ARRAY(0x331ee8), length=1, temp[0]=ARRAY(0x331ee8), temp[0] [0] =item1
at24: allHash{key1}[0]=item1
at25: allHash{key1}[1]=ARRAY(0x332020) 。allHash{key1}[1] [0]=
at26: allHash{key1}[0][0]=
at27: allHash{key1}[1][0]=
at28: allHash{key1}[0][1]=
奇怪的是,我的另一個程式中的這段幾乎相同的代碼卻能完美運行。
%hedgeHash = (); # collect the members of each hedge as an array, using stub as key
for (my $i=0; $i< @options; $i )
{ $Hstub = $options[$i][$iStub];
push @{$hedgeHash{$Hstub}}, $i; # hedgehash應該包含對沖成員的陣列。
}
更奇怪的是,如果我去掉push陳述句中的括號,我不再得到'item1'作為@temp和第14行和第24行的輸出,而是得到另一個陣列! WTF??
uj5u.com熱心網友回復:
請看下面的示例代碼,它演示了陣列哈希的使用。
的確,OP的編碼風格使代碼閱讀變得有些困難。
use strict;
use warnings;
use feature 'say';
使用 Data::Dumper。
my %allHash;
my $key1 = 'key1'/span>;
my $item1 = 'item1';
my $item2 = 'item2';
my $item3 = 'item3';
my $item4 = 'item4';
my $item5 = 'item5';
push @{$allHash{$key1}}, $item1;
push @{$allHash{$key1}}, $item2;
$allHash{$key1}[2] = $item3;
$allHash{$key1}[3] = [$item4,$item5];
say Dumper(\%allHash)。
輸出
$VAR1 = {
'key1' => [
'item1',
'item2'。
'item3'。
[
'item4'。
'item5', 'item5'.
]
]
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/311323.html
標籤:
上一篇:如何使用perlCGI列印哈希值
