in reply to Re^6: Keeping Order with YAML::XS (sort keys)
in thread Keeping Order with YAML::XS

You completely lost me with what your examples were demonstrating. But since Perl hashes offer no ordering guarantees, YAML::XS and YAML::Tiny should feel free to sort the keys however they wish before output.

Since Perl has no native ordered mapping, one from CPAN (or elsewhere) must be used. And by necessity it will have to be morphed into an array of individual hash maps before it can be sent out to YAML, and the process reversed upon load. Either that or one must be content to just operate on an array of hash maps directly in code... ugh

  • Comment on Re^7: Keeping Order with YAML::XS (sort keys)

Replies are listed 'Best First'.
Re^8: Keeping Order with YAML::XS (sort keys)
by Anonymous Monk on Jul 29, 2013 at 10:05 UTC

    ... lost ... demonstrating..

    that sort was used to sort keys from OPs structure, sorted to BREAK the hard work of Tie::IxHash -- hashes may not have order, but Tie::IxHash keeps order, the YAML modules use "sort keys" to throw away the work of Tie::IxHash , throw away the natural hash order,

      It was well documented in earlier posts that the ordering of Tie::IxHash was not preserved when serializing to YAML. It would be shocking if any of the CPAN YAML modules had special code to recognize an IxHash. Instead they just see it as a normal hash and proceed properly from there (ie. with freedom to sort keys). The solution has been presented above -- transform the IxHash into an array of hashes each containing a single key/value pair. That is a data structure that will work and answers the question posed by the OP.

      All that remains is for some code to be written.

      Still, a lot of code other than YAML::XS will ignore IxHash ordering...

      use Data::Dump qw(pp); use Tie::IxHash; tie my(%hash), 'Tie::IxHash', (a => 1, c => 3, b => 2); pp \%hash;
      Output:
      { # tied Tie::IxHash a => 1, b => 2, c => 3, }

        Still, a lot of code other than YAML::XS will ignore IxHash ordering..

        Yeah, with sort keys, and thats a bug