Hello Stefan, On Tue, Nov 15, 2011 at 08:10:12AM -0800, Stefan Wiederoder wrote: > Hello list, > > I´m using a json config file to read a file with server group > definitions, including group of groups like > this example: > > [jdoe@belbo]# cat groups.json > { > "G_Group_PR" : [ "serverA", "serverB" ], > "G_Group_QS" : [ "serverC", "serverD" ], > "G_All" : [ "G_Group_PR", "G_Group_QS" ] > } > > now I need to resolve all groups to their member servers to map > actions to them. > > this is where I´m stuck with an elegant solution, how can I > effectively parse this hash to > replace all Groups (always starting with G_)?
I'm assuming that you are using an existing CPAN[1] module to actually parse the JSON data into a Perl data structure. If you aren't then you should be. You did say "parse" so I wonder if maybe that is the part that you are stuck on. If so, don't worry about it, and just use an existing CPAN module to parse the JSON. Assuming you had an equivalent Perl data structure (loaded by a CPAN module) you could just do a lookup of groups when fetching the server name to see if more servers are needed: #!/usr/bin/perl use strict; use warnings; my %groups = ( G_Group_PR => [ qw/serverA serverB/ ], G_Group_QS => [ qw/serverC serverD/ ], G_All => [ qw/G_Group_PR G_Group_QS/ ], ); sub get_group_servers { my ($groups, $group) = @_; my @servers = @{$groups{$group}}; my %servers; for my $server (@servers) { if(exists $groups{$server}) { map { $servers{$_} = 1 } get_group_servers($groups, $server); } else { $servers{$server} = 1; } } return keys %servers; } for my $group (keys %groups) { my @servers = get_group_servers(\%groups, $group); print "Servers for group '$group':\n"; for my $server (@servers) { print " $server\n"; } print "\n"; } __END__ Output: Servers for group 'G_Group_QS': serverD serverC Servers for group 'G_Group_PR': serverA serverB Servers for group 'G_All': serverD serverC serverA serverB Just how you handle it depends on your use. Maybe it would be better to preprocess the data and "expand" the group references instead of looking them up at run-time, for example. If I'm mistaken about what problem you're having then please explain in more detail. :) Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bamccaig.com/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' [1] Comprehensive Perl Archive Network AKA cpan.org
signature.asc
Description: Digital signature