I think this is what you might be looking for:
#! /usr/bin/perl
use strict;
use Data::Dumper;
my (%routers);
#Load Router list
while(<DATA>){
last if( m/##GROUPS/ );
chomp;
my($name, $ip) = (split(/ /, $_))[1,3];
$routers{$name} = $ip;
}
my $group = 'group2'; #Get group to load here
my (%loaded_routers);
#Load requested group
while(<DATA>){
chomp;
s/ //g; #Clear spaces
my($groupname, $data) = split(/=/, $_);
if($groupname eq $group){
foreach my $n(split(/\,/,$data)){
$loaded_routers{$n} = $routers{$n};
}
last;
}
}
print Dumper \%loaded_routers;
__DATA__
router router1 = 192.168.1.2
router router2 = 192.168.1.3
router router3 = 192.168.1.4
router router4 = 192.168.1.5
router router5 = 192.168.1.6
##GROUPS
group1 = router1,router5
group2 = router1, router2, router3
group3 = router2, router3, router4
The Output:
$VAR1 = {
'router1' => '192.168.1.2',
'router3' => '192.168.1.4',
'router2' => '192.168.1.3'
};
Update:
AppConfig looks like a viable option as well. The above will work on the sample data that you gave, but
AppConfig is much more robust, and would probably be better it the data gets anymore complicated.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.