2015-11-13 42 views
-1

嗨,我在使用Perl的Graph::Directed中有一個問題。我想根據不同的索引更改數組名稱。我怎樣才能做到這一點只有一個循環,以改變索引更改數組名稱。以下是我想要展示的示例。數組中的循環

@v = $g-> strongly_connected_component_by_index(0); 
@v1 = $g-> strongly_connected_component_by_index(1); 
@v2 = $g-> strongly_connected_component_by_index(2); 

您的回覆非常感謝。謝謝。

+0

'我@AoA =:

my @components_by_index; for (0..2) { $components_by_index[$_] = [ $g->strongly_connected_component_by_index($_) ]; } print "$components_by_index[0][0]\n"; 

馬克·傑森·多明斯在不使用變量值作爲變量名寫了一個神話般的三部分故事map [$ g-> strong_connected_component_by_index($ _)],0..2;'http://perldoc.perl.org/perldsc.html#Declaration-of-an-ARRAY-OF-ARRAYS –

+3

你不想要動態變量名稱,這是一個壞主意。它不適用於'嚴格使用',一方面,這真的很糟糕。當你想要動態名稱時,你通常需要一個哈希:'$ hash {'name'} ='Fred''。但在你的情況下,數組的數組會更簡單。 '$ AoA [$ index] = [$ g-> ....($ index)]'。 – TLP

+0

我與@TLP,這裏有一個很好的3部分故事,爲什麼你想要使用哈希代替:http://perl.plover.com/varvarname.html – stevieb

回答