I'm successfully parsing a cisco config file, and grabbing the sections of config between each marker (cisco uses the ! symbol) using a multi line regex of:
/(search string)/i .. /^!/
My code looks like:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my (@results, @data) ;
#Test data to simulate a while loop on a file-handle running through a config file.
@data = (
"vlan 81" ,
" name Vlan 81 test1" ,
"!" ,
"vlan 82" ,
" name Vlan 82 test2" ,
"!" ,
"vlan 83" ,
" name Vlan 83 test3" ,
"!"
);
foreach ( @data ) {
if ( /vlan/i .. /^!/ ) {
push (@results , $_) ;
}
}
print Dumper ( @results ) . "\n" ;
exit;
It works really well, but I want to push the results into a hash, with each section of code being an anonymous array, so the results would look something like:
%Vlan -> [Vlan 81, name Vlan 81 test1] , [Vlan 82, name Vlan 82 test2] , [Vlan 83, name Vlan 83 test3]
But I can't work out how to do it, my code matches per line between the search string and the marker and I just end up rebuilding the results into another array, line by line.
Any help is much appreciated.
Cheers,
Andy
Vlan
. For this key you have a list of arrays. Don't you mean a hash where for each key (e.g. Vlan 81) you have a value? – Matteo