這是一個簡單的問題,但它讓我難倒,谷歌只是讓我誤入歧途。我想要做的就是打印一個散列的名字。例如:如何在Perl中打印散列名稱?
&my_sub(\%hash_named_bill);
&my_sub(\%hash_named_frank);
sub my_sub{
my $passed_in_hash = shift;
# do great stuff with the hash here
print "You just did great stuff with: ". (insert hash name here);
}
我不知道的部分是如何獲取括號中的內容(插入...)。對於嵌套散列,你可以使用「keys」標籤來獲得散列名稱(如果你想稱它們的話)。我無法弄清楚如何獲得整個散列名稱,但它似乎只是另一個關鍵。
你不能。子無法知道散列的名稱。 – 2012-08-10 17:55:51
哈希沒有名稱,但指向它的變量,看看這篇文章,看看如何訪問變量名http://stackoverflow.com/questions/5199860/get-variable-name- as-string-in-perl – hackattack 2012-08-10 17:57:05
我不認爲這是個好主意。這是Perl中可能的一種,但這並不意味着你應該這樣做。 – zostay 2012-08-10 18:16:04