yaml file conversion
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?
Thanks!
Jim
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
On 11-03-03 05:40 PM, Jim Green wrote:
> But is there a easier way of
> doing this I might not be aware of?
Given your brief description, no. The problem is that you can't output
the first datum without reading the last, because the last may be the
first thing that needs to be outputted.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
On Mar 3, 5:44=A0pm, shawnhco... [at] gmail.com (Shawn H Corey) wrote:
> On 11-03-03 05:40 PM, Jim Green wrote:
>
> > But is there a easier way of
> > doing this I might not be aware of?
>
> Given your brief description, no. =A0The problem is that you can't output
> the first datum without reading the last, because the last may be the
> first thing that needs to be outputted.
Hello, let me generalize this problem,
lets say I have a hash with 2 levels of keys,
I want to convert this hash to another hash but with the 2 levels of
keys reversed.. I hope there is a module or sth that can do it.
Hope this makes the problem clearer.
Thanks!
Jim
>
> --
> Just my 0.00000002 million dollars worth,
> =A0 =A0Shawn
>
> Confusion is the first step of understanding.
>
> Programming is as much about organization and communication
> as it is about coding.
>
> The secret to great software: =A0Fail early & often.
>
> Eliminate software piracy: =A0use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
On 3/3/11 Thu Mar 3, 2011 3:11 PM, "Jim Green"
<student.northwestern [at] gmail.com> scribbled:
> On Mar 3, 5:44=A0pm, shawnhco... [at] gmail.com (Shawn H Corey) wrote:
>> On 11-03-03 05:40 PM, Jim Green wrote:
>>
>>> But is there a easier way of
>>> doing this I might not be aware of?
>>
>> Given your brief description, no. =A0The problem is that you can't output
>> the first datum without reading the last, because the last may be the
>> first thing that needs to be outputted.
>
> Hello, let me generalize this problem,
> lets say I have a hash with 2 levels of keys,
> I want to convert this hash to another hash but with the 2 levels of
> keys reversed.. I hope there is a module or sth that can do it.
That is a simple problem that doesn't need a module (although one may
exist).
Untested:
my %oldhash =3D ( a =3D> { b=3D>c, d=3D>e }, f =3D> {g=3D>h, i=3D>j}, ... );
my %newhash;
for( my($key1,$val1) =3D each %oldhash ) {
for( my($key2,$val2) =3D each %{$val1} ) {
$newhash{$key2}->{$key1} =3D $val2;
}
}
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
On Thu, Mar 3, 2011 at 5:40 PM, Jim Green
<student.northwestern [at] gmail.com> wrote:
> manually populate another hash and dump. But is there a easier way of
> doing this I might not be aware of?
I don't know if I'd call it easier. I'm not personally familiar with
YAML so I only bothered to parse the example that you gave.
#!/usr/bin/env perl
use strict;
use warnings;
my %inverted_data;
my $subkey;
while(my $line = <DATA>)
{
chomp $line;
if(my ($key) = $line =~ /^([^:]+):$/)
{
$subkey = $key;
}
if(my ($key, $value) = $line =~ /^\s*([^:]+):([^:]+)$/)
{
$inverted_data{$key}->{$subkey} = $value;
}
}
for my $key (keys %inverted_data)
{
print "$key:\n";
for my $subkey (keys %{$inverted_data{$key}})
{
print " $subkey:$inverted_data{$key}->{$subkey}\n";
}
}
__DATA__
key1:
a: value1
b: value1
key2:
a: value2
b: value2
If a module exists to parse YAML then you might as well use it (unless
there's a good technical reason not to). Some Perl veterans can
probably reduce this to couple of lines. :P
--
Brandon McCaig <http://www.bamccaig.com/> <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence.org>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
On Thu, Mar 3, 2011 at 6:39 PM, Brandon McCaig <bamccaig [at] gmail.com> wrote:
> I don't know if I'd call it easier. I'm not personally familiar with
> YAML so I only bothered to parse the example that you gave.
Actually looking into YAML it seems much more complicated than this so
unless you know for sure that the input complexity is simple (and have
a good technical reason to manually parse it), I would encourage you
to use a module instead. That said, it seems non-trivial to determine
how to invert the keys on an undetermined data structure, so it might
be too much to ask to write this program with an arbitrary YAML
structure without defining the exact requirements (and justification).
(As a side note, I erroneously removed indentation from the source
data in my sample program when I pasted it into Vim, but the program
will work even with the indentation)
--
Brandon McCaig <http://www.bamccaig.com/> <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence.org>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
On Thu, Mar 3, 2011 at 6:31 PM, Jim Gibson <jimsgibson [at] gmail.com> wrote:
> On 3/3/11 Thu =A0Mar 3, 2011 =A03:11 PM, "Jim Green"
> <student.northwestern [at] gmail.com> scribbled:
>
>> On Mar 3, 5:44=A0pm, shawnhco... [at] gmail.com (Shawn H Corey) wrote:
>>> On 11-03-03 05:40 PM, Jim Green wrote:
>>>
>>>> But is there a easier way of
>>>> doing this I might not be aware of?
>>>
>>> Given your brief description, no. =A0The problem is that you can't outp=
ut
>>> the first datum without reading the last, because the last may be the
>>> first thing that needs to be outputted.
>>
>> Hello, let me generalize this problem,
>> lets say I have a hash with 2 levels of keys,
>> I want to convert this hash to another hash but with the 2 levels of
>> keys reversed.. I hope there is a module or sth that can do it.
>
> That is a simple problem that doesn't need a module (although one may
> exist).
>
> Untested:
>
> my %oldhash =3D ( a =3D> { b=3D>c, d=3D>e }, f =3D> {g=3D>h, i=3D>j}, ...=
);
> my %newhash;
> for( my($key1,$val1) =3D each %oldhash ) {
> =A0for( my($key2,$val2) =3D each %{$val1} ) {
> =A0 =A0$newhash{$key2}->{$key1} =3D $val2;
> =A0}
> }
Thanks Jim, this will definitely work. but changing order of keys is a
general problems and hopefull some module exists to do that.
Jim
>
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
Thanks Brandon, YAML should get some functions to manipulate it..
On Thu, Mar 3, 2011 at 7:56 PM, Brandon McCaig <bamccaig [at] gmail.com> wrote:
> On Thu, Mar 3, 2011 at 6:39 PM, Brandon McCaig <bamccaig [at] gmail.com> wrote:
>> I don't know if I'd call it easier. I'm not personally familiar with
>> YAML so I only bothered to parse the example that you gave.
>
> Actually looking into YAML it seems much more complicated than this so
> unless you know for sure that the input complexity is simple (and have
> a good technical reason to manually parse it), I would encourage you
> to use a module instead. That said, it seems non-trivial to determine
> how to invert the keys on an undetermined data structure, so it might
> be too much to ask to write this program with an arbitrary YAML
> structure without defining the exact requirements (and justification).
>
> (As a side note, I erroneously removed indentation from the source
> data in my sample program when I pasted it into Vim, but the program
> will work even with the indentation)
>
>
> --
> Brandon McCaig <http://www.bamccaig.com/> <bamccaig [at] gmail.com>
> V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
> Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence.org>
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
>>>>> "JG" =3D=3D Jim Green <student.northwestern [at] gmail.com> writes:
JG> On Thu, Mar 3, 2011 at 6:31 PM, Jim Gibson <jimsgibson [at] gmail.com> wro=
te:
>> On 3/3/11 Thu =A0Mar 3, 2011 =A03:11 PM, "Jim Green"
>> <student.northwestern [at] gmail.com> scribbled:
>>
>>> On Mar 3, 5:44=A0pm, shawnhco... [at] gmail.com (Shawn H Corey) wrote:
>>>> On 11-03-03 05:40 PM, Jim Green wrote:
>>>>
>>>>> But is there a easier way of
>>>>> doing this I might not be aware of?
>>>>
>>>> Given your brief description, no. =A0The problem is that you can't o=
utput
>>>> the first datum without reading the last, because the last may be the
>>>> first thing that needs to be outputted.
>>>
>>> Hello, let me generalize this problem,
>>> lets say I have a hash with 2 levels of keys,
>>> I want to convert this hash to another hash but with the 2 levels of
>>> keys reversed.. I hope there is a module or sth that can do it.
>>
>> That is a simple problem that doesn't need a module (although one may
>> exist).
>>
>> Untested:
>>
>> my %oldhash =3D ( a =3D> { b=3D>c, d=3D>e }, f =3D> {g=3D>h, i=3D>j}, =
.... );
>> my %newhash;
>> for( my($key1,$val1) =3D each %oldhash ) {
>> =A0for( my($key2,$val2) =3D each %{$val1} ) {
>> =A0 =A0$newhash{$key2}->{$key1} =3D $val2;
>> =A0}
>> }
JG> Thanks Jim, this will definitely work. but changing order of keys is a
JG> general problems and hopefull some module exists to do that.
hash keys have no order. this is true even when serialized to a string
in any format (yaml, dumper, json, etc.). when read back in, the order
in the string will be lost in most any lang. so expecting order there is
a bad idea.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
>>>>> "JG" == Jim Green <student.northwestern [at] gmail.com> writes:
JG> Thanks Brandon, YAML should get some functions to manipulate it..
that doesn't make sense. yaml and other serializers don't manipulate
data. they convert in memory data to a string format that can be saved,
printed, send on a socket, transfered between languages, etc. these
serializers are not data manipulators. you use the language to
manipulate the data in memory before or after you serialize it with
yaml.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: yaml file conversion
On 04/03/2011 01:00, Jim Green wrote:
>
> Thanks Jim, this will definitely work. but changing order of keys is a
> general problems and hopefull some module exists to do that.
I think you are overestimating the generality of this problem. I cannot
remember seeing it before, apart from a simple reversal of key/value
pairs in a hash. In any case the code to implement it is so trivial that
a module is unnecessary. My advice is to stick with the coded solution.
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/