On 03/03/2011 22:40, Jim Green wrote:
Hello:
I have a yaml file

key1:
   a: value1
   b: value1
key2:
   a: value2
   b: value2

I want it converted to

a:
     key1:value1
     key2:value2
b:
     key1:value1
     key2:value2

I could use YAML module to load the first yaml file to a hash and
manually populate another hash and dump. But is there a easier way of
doing this I might not be aware of?

I'm not sure what could be easier than doing exactly what you describe. The program below couldn't be much smaller.

HTH,

Rob


use strict;
use warnings;

use YAML::XS;

my $data = Load <<DATA;
key1:
  a: value1
  b: value1
key2:
  a: value2
  b: value2
DATA

my $transform;

foreach my $k1 (keys %$data) {
  foreach my $k2 (keys %{$data->{$k1}}) {
    $transform->{$k2}{$k1} = $data->{$k1}{$k2};
  }
}

print Dump $transform;

**OUTPUT**

---
a:
  key1: value1
  key2: value2
b:
  key1: value1
  key2: value2

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to