快速破解。这将读入一组值(由两个连续的换行符分隔),将其放入哈希中,然后将该哈希推送到数组中。使用输入记录分隔符$/
读取记录。设置$/
为""
空字符串有点像将其设置为"\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