in reply to Re^4: Keeping Order with YAML::XS
in thread Keeping Order with YAML::XS

The ordered data structure you're dreaming of is an array. It is an ordered sequence and there is an example of it in the data structure created when parsing either YAML format of the data in question. If you want to create an ordered sequence in the YAML output, use a Perl array.

From YAML spec 1.2:

# Ordered maps are represented as # A sequence of mappings, with # each mapping having one key --- !!omap - Mark McGwire: 65 - Sammy Sosa: 63 - Ken Griffy: 58

Which produces this Perl data structure

[ { "Mark McGwire" => 65 }, { "Sammy Sosa" => 63 }, { "Ken Griffy" => 58 }, ]

So lets duplicate that with your data

[ { path => "/export/home/frank" }, { options => [ { param => "i", value => 1001 }, { param => "f" }, { param => "x", value => "eyes" }, {}, { param => "b", value => "toes" }, { param => "p" }, ], }, { arguments => [{ value => "test" }] }, ]
Which produces the format suggested by the YAML spec:
--- - path: /export/home/frank - options: - param: i value: '1001' - param: f - param: x value: eyes - {} - param: b value: toes - param: p - arguments: - value: test

So we see how to create ordered mappings in both YAML and in Perl. YAML::XS is doing exactly the right thing because Perl hashes are not ordered, therefore it outputs them as unordered mappings. So the real question is which Perl data structure you're going to use to represent Ordered Mappings, because Perl hashes can't do that job and arrays of individual hashes aren't convenient.

Several people have suggested Tie::IxHash and this is a very good choice. But since YAML::XS sees it as just a regular hash, it outputs in unordered YAML notation. You will have to preprocess your data structures to convert IxHash's into arrays of individual mappings and reverse the process when loading.

Replies are listed 'Best First'.
Re^6: Keeping Order with YAML::XS (sort keys)
by Anonymous Monk on Jul 29, 2013 at 09:44 UTC
    YAML::XS and YAML::Tiny both clearly sort keys
    $ perl -le " my %f = qw/ path 1 options 1 arguments 1 /; print join q +/ /, keys %f; " options arguments path $ perl -le " my %f = qw/ path 1 options 1 arguments 1 /; print join q +/ /, sort keys %f; " arguments options path

    No other way to get arguments options path

    $ perl -le " my %f = qw/ arguments 1 options 1 path 1 /; print join q/ + /, keys %f; " options path arguments $ perl -le " my %f = qw/ arguments 1 options 1 path 1 /; print join q/ + /, sort keys %f; " arguments options path

      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

        ... 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,