下面的脚本列出了分析一个配置文件中的所有作业,并只取出了运行作业的可执行文件-- perl或shell脚本。然后打开可执行文件,逐行遍历它,取出它读取的文件路径和输出的路径。
#!/usr/bin/perl
use strict;
use warnings;
open my $fh_rmancfg, '<', "/data/autosys-us.cfg"
or die "can't open file $!" ;
while ( my $cfg_line = <$fh_rmancfg>) {
if ($cfg_line =~ /executable/) {
my $cut_cfg_line = substr "$cfg_line", 13 ;
if ($cut_cfg_line =~ /(\/\S*\.(sh|pl|ksh))/) {
chomp($cut_cfg_line);
open my $fh_cut_cfg, '<', $cut_cfg_line
or warn qq{Can't open file "$cut_cfg_line" : $! } ;
while (my $path = <$fh_cut_cfg>) {
if ($path =~ /(\"\/\S*)\"/) {
print "$cut_cfg_line ---> $path";
}
}
}
}
}这很好,除非我把这些值输入到散列中,键就会被覆盖--请注意,有时一个键有一个值,有时一个键有零个或多个值。
program_1 --> filepath_one
program_1 --> filepath_two
program_1 --> filepath_three
program_2 --> filepath_one
program_2 --> filepath_two
program_2 --> filepath_three
program_2 --> filepath_four
program_3 --> filepath_one
program_4 --> filepath_one
program_4 --> filepath_one
program_4 --> filepath_two
program_5 --> filepath_one
program_5 --> filepath_two
program_5 --> filepath_thee
program_5 --> filepath_four
program_5 --> filepath_five
program_5 --> filepath_six
program_5 --> filepath_seven
program_5 --> filepath_eight我需要一个数据结构,它将输入一个唯一的键,但是有许多值。我试图弄清楚这个结构是什么,以及如何将值输入到结构中。
program_1 --> filepath_one
--> filepath_two
--> filepath_three
program_2 --> filepath_one
--> filepath_two
--> filepath_three
--> filepath_four
program_3 --> filepath_one
program_4 --> filepath_one
--> filepath_one
--> filepath_two
program_5 --> filepath_one
--> filepath_two
--> filepath_thee
--> filepath_four
--> filepath_five
--> filepath_six
--> filepath_seven
--> filepath_eight发布于 2014-09-17 17:04:47
正如你所发现的,当你做这样的事情时:
$hash{key} = "value";
$hash{key} = "new value";原来的价值已经消失了。幸运的是,Perl 5允许您使用参考文献。Perl引用允许您执行如下操作:让哈希键指向值数组而不是单个值。
$hash{key} = []; # The value is a reference to an array
$hash{key}->[0] = "value";
$hash{key}->[1] = "new value";现在,$hash{key}包含两个值!数组被称为引用,有时甚至是匿名数组,因为数组本身没有与之关联的变量名。
您可以通过在变量前面放一个反斜杠来创建引用:
$array_reference =\@数组;
要取消引用(也就是说,将引用转换回数组),您可以将正确的警号放在它前面:
我的@new_array = @{ $array_reference };
使用引用允许Perl使用更为复杂的数据结构。您可以使用push将值推送到特定哈希值指向的数组引用中,而不是简单地将值分配给哈希:
my %hash;
$hash{key} = []; # This key is an array reference
for each $value ( qw(value1 value2 value3 ) ) {
push @{ $hash{key} }, $value;
}阅读Perl参考教程,它会让您了解如何使用数组的散列来存储所有的值。
发布于 2014-09-17 16:30:50
如果要有重复的键,则需要使用数组的散列。您可以在Perl数据结构烹饪本中阅读更多有关不同数据结构的信息。
下面是一个简单的例子:
#!/usr/bin/perl
use strict;
use warnings;
my %hashofArrays;
while (<DATA>) {
my ( $key, $value ) = split;
push @{ $hashofArrays{ $key } }, $value;
}
use Data::Dumper;
print Dumper \%hashofArrays;
__DATA__
program_1 filepath_one
program_1 filepath_two
program_1 filepath_three
program_2 filepath_one
program_2 filepath_two
program_2 filepath_three
program_2 filepath_four
program_3 filepath_one
program_4 filepath_one
program_4 filepath_one
program_4 filepath_two
program_5 filepath_one
program_5 filepath_two
program_5 filepath_thee
program_5 filepath_four
program_5 filepath_five
program_5 filepath_six
program_5 filepath_seven
program_5 filepath_eight输出:
$VAR1 = {
'program_5' => [
'filepath_one',
'filepath_two',
'filepath_thee',
'filepath_four',
'filepath_five',
'filepath_six',
'filepath_seven',
'filepath_eight'
],
'program_3' => [
'filepath_one'
],
'program_2' => [
'filepath_one',
'filepath_two',
'filepath_three',
'filepath_four'
],
'program_1' => [
'filepath_one',
'filepath_two',
'filepath_three'
],
'program_4' => [
'filepath_one',
'filepath_one',
'filepath_two'
]
};https://stackoverflow.com/questions/25895549
复制相似问题