Hi wtc,

Have been debating whether to post this or not. Was playing with this code while we were discussing your issue when a couple of people suggested using Tie::IxHash, but then found its ordering wasn't recognized by YAML::XS. Here it is for whatever it may be worth.

In an earlier post a quote from the YAML Spec shows the proper format for ordered mappings. Below is code to recursively convert a data structure containing IxHash's to one needed for output by YAML::XS. There is also a function to recursively convert a structure loaded from YAML::XS into one containing properly ordered IxHash's.

The code is presented as is, i'm sure there is a more elegant and less bug ridden version trying to get out -- would be happy to receive criticism. Mostly I hope it helps with your problem if you're still looking for a solution

use strict; use warnings; use Tie::IxHash; use Data::Dumper; use Data::Rmap qw(:types rmap_to); sub ixhash_to_yamlxs { my $item = \+shift; my $get_yaml_struct = sub { my $raw = shift; my $ix = tied(%$raw); return $raw unless defined $ix and $ix->isa('Tie::IxHash'); return [ map { { $_ => $raw->{$_} } } $ix->Keys ]; }; rmap_to { $_ = $get_yaml_struct->($_) } HASH, $item; return $item; } sub yamlxs_to_ixhash { my $item = shift; my $make_ixhash_from_array = sub { my $raw = shift; tie my(%hash), 'Tie::IxHash'; eval { no warnings; for (@$raw) { return $raw if scalar keys $_ ne 1; my ($key) = keys $_; $hash{$key} = $_->{$key}; } }; return $raw if $@; return \%hash; }; rmap_to { $_ = $make_ixhash_from_array->($_) } ARRAY, \$item; return $item; } tie my(%embed), 'Tie::IxHash', (j => 5, k => 6, l => 7); tie my(%hash), 'Tie::IxHash', (a => 1, c => { z => 26 }, b => 2, d => +[ 'j', 'k', 'l'], e => \%embed); my $out = ixhash_to_yamlxs \%hash; print Dumper $out; my $in = yamlxs_to_ixhash $out; print Dumper $in;
# IxHash converted for output by YAML::XS # Notice the embeded IxHash was handled # and also an embeded native hash (key 'c') # that is not converted to ordered structure \[ { 'a' => 1 }, { 'c' => { 'z' => 26 } }, { 'b' => 2 }, { 'd' => [ 'j', 'k', 'l' ] }, { 'e' => [ { 'j' => 5 }, { 'k' => 6 }, { 'l' => 7 } ] } ];
# ordered Tie::IxHash recovered from YAML::XS format \{ 'a' => 1, 'c' => { 'z' => 26 }, 'b' => 2, 'd' => [ 'j', 'k', 'l' ], 'e' => { 'j' => 5, 'k' => 6, 'l' => 7 } };

In reply to Re: Keeping Order with YAML::XS by Loops
in thread Keeping Order with YAML::XS by walkingthecow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.