快速入侵。这将读取一组值(由两个连续的换行符分隔),将其放入散列中,然后将该散列推送到数组上。使用输入记录分隔符$/
来读取记录。将$/
设置为""
空字符串有点像将其设置为"\n\n"
,请在上面链接的文档中阅读更多内容。
不太清楚你在这里需要什么。如果只有一行合并,只需存储散列而不将其推送到数组。它会“记住”第一条记录。
use strict;
use warnings;
$/ = ""; # paragraph mode
my @sets;
while (<DATA>) {
my %set;
chomp; # this actually removes "\n\n" -- the value of $/
for (split /\n/) { # split each record into lines
my ($set,$what,$value) = split ' ', $_, 3; # max 3 fields
$set{$what} //= $value; # //= means do not overwrite
}
$set{device} =~ s/^"|"$//g; # remove quotes
push @sets, \%set;
}
for my $set (@sets) { # each $set is a hash ref
my $string = join " ", "route",
@{$set}{"device","dst","gateway"}; # using hash slice
print "$string\n";
}
__DATA__
set device "internal"
set dst 13.13.13.13 255.255.255.255
set gateway 172.16.1.1
set device "internal"
set dst 14.14.14.14 255.255.255.255
set gateway 172.16.1.1
set device "internal"
set dst 15.15.15.15 255.255.255.255
set gateway 172.16.1.1
输出:
route internal 13.13.13.13 255.255.255.255 172.16.1.1
route internal 14.14.14.14 255.255.255.255 172.16.1.1
route internal 15.15.15.15 255.255.255.255 172.16.1.1
来源
2012-03-08 02:28:46
TLP
我认为它要在你的输出,'172.161.1.1'当你输入接近的唯一的事情就是'172.16.1.1'一个错字? – TLP 2012-03-07 23:18:20
你是对的,错字,对不起, – PhobicSTI 2012-03-07 23:19:11
你是否希望使用第一个出现的“dst”,也就是'13.13.13.13',或者你有其他的方法来选择你的输出? – TLP 2012-03-07 23:21:05