2011-12-19 55 views
1

我曾經閱讀過以下涉及迭代的Perl代碼。關於數據結構的說明

for my $j (0 .. $#{$dat[$Row]}) 
{ 
    $vectors{ $dat[$Row][$j] } = $j; 
} 

什麼

$vectors{ $dat[$Row][$j] } 

立場? 這相當於$vectors->$dat[$Row][$j]

+1

你的標題可以大大改善 - 這是什麼迭代有什麼關係呢? – reinierpost 2011-12-20 09:56:19

回答

4
what does $vectors{ $dat[$Row][$j] } stand for? 

$dat[$Row]是對數組的引用。 $dat[$Row][$j]顯然是該陣列中的一個元素。無論它包含什麼值,都將成爲%vectors中的散列鍵,其值爲$j

Is that equivalent $vectors->$dat[$Row][$j] 

不,那將參照變量$vectors,不%vectors

更可讀的方式來寫,這可能是:

my $aref = $dat[$Row]; 
for my $index (keys @$aref) { 
    my $key = $aref->[$index]; 
    $vectors{$key} = $index; 
} 

這也體現了利用->,取消引用的參考。

1

$vectors是一個散列,$dat一個多維數組(參考數組)和$Row$j兩個標量。因此,您將$dat[$Row][$j]中的密鑰設置爲%vectors哈希值爲$j

0

%vectors是一個散列。
$vectors{$k}爲關鍵$k
$dat[$Row][$j]中的散列值是一個2-d數組的一個元素(列$j,行$Row

所以環路創建散列其中鍵是內容和值是列索引。

0
$vectors{ $dat[$Row][$j] } 

是短期的

$vectors{ $dat[$Row]->[$j] } 

如果你拼寫出來,

# $Row is a row index. 
# $j is a column index. 
# (How inconsistent!) 

my $row = $dat[$Row]; # A ref to an array. 
my $key = $row->[$j]; # A value from the table. 
$vectors{$key}