Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^4: Keeping Order with YAML::XS

by walkingthecow (Friar)
on Jul 29, 2013 at 08:08 UTC ( #1046791=note: print w/replies, xml ) Need Help??

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

Ahhh, yes! But, here's the thing. How do I create a data structure that is ordered. I mean, I realize that hashes are unordered, but I'd like to create an ordered YAML sequence from a data structure. The problem is, an ordered YAML sequence looks the same Perl hash structure wise (from input) as an unordered YAML on output. I need the YAML to be ordered, and my issue is creating the data structure that makes that.

Replies are listed 'Best First'.
Re^5: Keeping Order with YAML::XS
by Corion (Pope) on Jul 29, 2013 at 08:10 UTC

    I would think that using a pure Perl YAML encoder together with Tie::IxHash should keep the order of the keys. See AM's reply below, that doesn't seem to work :-(

    If your data structure is reasonably fixed, you might even get through by using a text template and filling in the values. Usually that means having a machine-readable data schema to generate the template first.

Re^5: Keeping Order with YAML::XS
by Loops (Curate) on Jul 29, 2013 at 09:40 UTC

    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.

      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046791]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2017-10-22 09:00 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (272 votes). Check out past polls.