對於所有這些愚蠢的問題感到抱歉,我一直強調Perl編程,而且我發現很難像Perl程序員那樣思考。今天如何將變量的值用作Perl變量名稱?
愚蠢的問題: 我使用id字段作爲關鍵,像這樣
#open file
my %hash;
while (<MY_FILE>) {
chomp;
my ($id, $path, $date) = split /\|/;
$hash{$id} = {
"path" => $path,
"date" => $date
};
}
加載管道分隔文件轉換成散列有幾次,但是,當我真正需要的關鍵是路徑,因爲無論出於何種原因(並且不能改變),id不是唯一的,所以我有一個明智的想法,我可以將它全部放入一個子例程中,並傳遞變量的名稱以使用作爲它的關鍵,有點像這樣:
load_hash("path");
sub load_hash {
my $key = shift;
#do stuff, and then in while loop
$hash{${$key}} = #and so on
}
但perldb x $ {$ key}始終是undef,儘管x $ {path}將值打印到$ path中。
有沒有某種方式做我想要做的?
TIA
謝謝,工作。 – Sparkles 2009-11-13 00:57:26