我有一個perl腳本,我正在讀取給定目錄中的文件,然後將這些文件放入數組中。然後,我希望能夠將這些數組元素移動到perl哈希中,其中數組元素是哈希值,併爲每個哈希值自動分配數字鍵。自動增加perl散列中的數字鍵值?
下面的代碼:
# Open the current users directory and get all the builds. If you can open the dir
# then die.
opendir(D, "$userBuildLocation") || die "Can't opedir $userBuildLocation: $!\n";
# Put build files into an array.
my @builds = readdir(D);
closedir(D);
print join("\n", @builds, "\n");
此打印出來:
test.dlp
test1.dlp
我想利用這些值,並將其插入到一個哈希,看起來就像這樣:
my %hash (
1 => test.dlp
2 => test1.dlp
);
我希望數字鍵可以自動遞增,具體取決於在給定目錄中可以找到多少個文件。
我只是不確定如何讓自動遞增鍵設置爲散列中每個項目的唯一數值。
第一個選項完美的作品!非常感謝。 –