本文介绍了我如何在我的Perl脚本中填充散列(已经在单独的文件中定义)并对其执行必要的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的Perl脚本中填充散列(已在单独文件中定义)并对其执行必要的操作?



例如

p>

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'=>'在Linux中构建强大基础的实际示例',
'nagios core'=> '监控一切,积极主动,安然入睡'
},
);


解决方案

@Gibron其实已经回答了您的问题。 >
因此,我只是向您展示代码,您可能会更感兴趣。

填充普通散列的方式与填充散列散列的方式相同。 >
我使用Data :: Dumper直接显示哈希结构,您可以选择自己的方式来了解最终哈希包含的内容。

 使用strict; 
使用Data :: Dumper qw(Dumper);
做'file1.def'; #评估文件1

#将新的子密钥和值添加到'哈希散列'
$ file1 :: tgs {'文章'} {'emacs'} = '21发布了很棒的文章' ;

#创建一对全新的
$ file1 :: tgs {'new_key'} {'new_sub_key'} ='new_value';

#查看结果
print Dumper(\%file1 :: tgs);


How do I populate a hash (that has been defined in a separate file) in my Perl script and do necessary operations on it?

For ex:

file1.pl -- contains the defined hash,

file2.pl -- user defined code that should populate the hash from 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 actually has already answered your question.
So I just show you the code, which you may be more interested.
The way of populating an ordinary hash is the same with that of populating a 'hash on hash'.
I'm using Data::Dumper to show the hash structure directly, you can choose your own way to know what the final hash contains.

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);

这篇关于我如何在我的Perl脚本中填充散列(已经在单独的文件中定义)并对其执行必要的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 21:57