[英]How do I populate a hash (that has been defined in a separate file) in my Perl script and do necessary operations on it?
如何在Perl腳本中填充哈希(已在單獨的文件中定義),並對其進行必要的操作?
例如:
file1.pl-包含定義的哈希,
file2.pl-用戶定義的代碼,應填充file1.pl中的哈希
my %tgs = (
'articles' => {
'vim' => '20 awesome articles posted',
'awk' => '9 awesome articles posted',
'sed' => '10 awesome articles posted'
},
'ebooks' => {
'linux 101' => 'Practical Examples to Build a Strong Foundation in Linux',
'nagios core' => 'Monitor Everything, Be Proactive, and Sleep Well'
},
);
@Gibron實際上已經回答了您的問題。
因此,我只向您顯示代碼,您可能對此更感興趣。
填充普通哈希的方式與填充“哈希哈希”的方式相同。
我正在使用Data :: Dumper直接顯示哈希結構,您可以選擇自己的方式來了解最終哈希包含的內容。
use strict;
use Data::Dumper qw(Dumper);
do 'file1.def'; # evaluate file1
# add new sub key and value to 'hash of hash'
$file1::tgs{'articles'}{'emacs'} = '21 awesome articles posted';
# create a completely new pair
$file1::tgs{'new_key'}{'new_sub_key'} = 'new_value';
# see the result
print Dumper (\%file1::tgs);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.