On Tuesday, November 15, 2011 at 11:10 , 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 think you need to explain your desired outcome a bit further. I'm going to assume you've got the JSON -> Perl data structure bit down, and what you want to do is ask for 'G_All' and get back qw/ serverA serverB serverC serverD / -- is that correct?

If so, the big question becomes, how many levels of reference are you going to support? If it's just a single level, like in your example, it's pretty easy:

my %resolved_hash;

foreach my $key ( keys %inital_hash ) {
foreach my $val ( @{ $initial_hash{$key} } ) {
if( $val =~ /^G_/ ) {
push @{ $resolved_hash{$key} } , @{ $inital_hash{$key} };
else { push @{ $resolved_hash{$key} } , $val }

(Caveat: untested, written while having first cup of coffee.)

If you want to support more than one level, you're going to want to have something recursive -- dive down until there aren't any 'G_*' keys left, then unwind.


Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 6 | next ›
Discussion Overview
groupbeginners @
postedNov 15, '11 at 4:11p
activeNov 17, '11 at 9:09p



site design / logo © 2022 Grokbase