Quick hack. This will read in a set of values (separated by two consecutive newlines), put it in a hash, then push that hash onto an array. Using input record separator $/
to read records. Setting $/
to ""
the empty string is somewhat like setting it to "\n\n"
, read more in the documentation linked above.
Not quite sure what you need here. If its just one line merged, simply store the hash without pushing it to an array. It will "remember" the first record.
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
Output:
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