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.


In reply to Re^5: 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.