On Fri, May 07, 2010 at 12:44:01PM -0400, Bob McConnell wrote: > From: Tom > > > I'm having trouble merging YAML streams. > > > > Basic premise is that I load multiple YAML files and I want to combine > > the result. There may be common elements within subsequent YAML files > > and I would want the last loaded to be the taken value if one already > > existed. > > > > I have tried treating the YAML nodes like hashes: > > eg $yaml = ($yaml1, $yaml2) > > but this does not merge them. > > Also there may be more than 2 streams, but I would like to cascade > > merge them as I loop through a list of YAML files to load (eg > > $yaml=($yaml, $next_yaml)) but this gives me a void context. > > > > Can anyone suggest how I could merge them? > > You might try the suggestion here: > <http://www.perlmonks.org/?node_id=813443>. As usual, I ran across it > yesterday while looking for something else. > > Bob McConnell > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > >
OK. Seems I still cannot merge two complex yaml nodes. Taking some inspiration from the link above I can traverse the two nodes and see what is a has and an array, but am still unable to merge them. In fact, I seem to be able to traash my starting array of yaml nodes and just return the last array processed! This is some experimental code I am trying to create a sub from: -------------------- CODE -------------------- #!/usr/bin/perl use YAML::Syck; use Data::Dumper; my ($yaml) = Load(<<'...'); --- nameserver: - 172.23.0.5 - 172.23.0.6 randomvar: - elastic - orange - clint network: - eth0: - ip: 192.168.7.2 - netmask: 255.255.255.0 ... #print "yaml: \n", Dump($yaml); my ($yaml2) = Load(<<'...'); --- nameserver: - 192.168.0.1 - 192.168.0.2 #network: # - eth0: # - ip: 172.23.10.141 # - netmask: 255.255.240.0 ... #print "yaml2: \n", Dump($yaml2); print "\n\n"; my $return = &mergekeys($yaml2, $yaml); print "\nreturn: \n", Dump($return); sub mergekeys { my ($orig) = @_; while (my $ref = shift) { print $ref, "\n"; my $type = ref $ref; if ( $type eq 'HASH' ) { print "HASH: ", Dumper($ref); # need to do something here to merge hashes... print "hashREF: ", Dumper(%$ref); push @_, grep {ref eq 'HASH' or ref eq 'ARRAY'} values %$ref; print "Pushed hash!\n"; } # end if hash elsif ($type eq 'ARRAY') { # print "\n", Dumper($ref); print "arrayREF: ", Dumper(@$ref); push @_, grep {ref eq 'HASH' or ref eq 'ARRAY'} @$ref; print "Pushed array!\n"; } # end if array } # end while return $orig; } # end sub -------------------- END CODE -------------------- There is a section in the middle that needs a merging of hashes and I am not sure how to pull the hash key out and then merge it back into %$ref. Any help would be grand. Tom -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/