Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: YAML newbie help

by Tortue (Scribe)
on May 31, 2005 at 10:00 UTC ( [id://461977]=note: print w/replies, xml ) Need Help??


in reply to Re: YAML newbie help
in thread YAML newbie help

My humble apologies for wasting your time. How silly of me not to see that the answer was contained in my own question! I of course didn't mean the message was nonsense. I meant the nonsense was in my behavior. I foolishly thought there might be some list of examples like the Perl data structures cookbook, but for YAML, that my poor searching abilities hadn't uncovered. And that next time someone searched for YAML in this site, they'd be happy to find this node. I'll do penance for my presumption.

I'm sorry I wasn't more specific. My original, very trivial problem was to have a list of days, with for each day a team of people, and a head of a team.

By trial and error, I found this:

days: - date: 2005-06-01 team: - Jack - Jill - Joe boss: Clara - date: 2005-06-02 team: - Harry - Hillary - Howie boss: Victoria
It does the trick, producing an array of hashes of arrays. Now (does my greed know no bounds?) I'm wondering if there's a more concise way, so that team members could be on the same line. At the same time I feel shame to even mention it, because I'm sure the answer is in the fine documentation, and it's just my poor eyes that don't see it! But perhaps wisdom can be achieved through shame.

Replies are listed 'Best First'.
Re^3: YAML newbie help
by Tortue (Scribe) on May 31, 2005 at 10:18 UTC
    Indeed, answering my own question after reading more of the long, but well-written specification: a more concise, but equivalent version, is of course:
    - date: 2005-06-01 team: [ Jack, Jill, Joe ] boss: Clara - date: 2005-06-02 team: [ Harry, Hillary, Howie ] boss: Victoria
    Even more concise, each day can be made to fit on one line:
    - { date: 2005-06-01, team: [ Harry, Hillary, Howie ], boss: Clara + } - { date: 2005-06-02, team: [ Jack, Jill, Joe ], boss: Victor +ia }
    which makes it more readable as a table.

    I wonder if you can simplify even further, by factoring out the hash keys? Something on the order of:

    # INCORRECT YAML SYNTAX - { date:, team:, boss: } - { 2005-06-01, [ Harry, Hillary, Howie ], Clara } - { 2005-06-02, [ Jack, Jill, Joe ], Victoria }
    Probably not.
      I see. I always start with `perldoc YAML', so I would've approached the problem the same way grinder would, akin to
      use YAML; my( @foo ) = ({ 'date' => '2005-06-01', 'boss' => 'Clara', 'team' => [ 'Jack', 'Jill', 'Joe' ] }, { 'date' => '2005-06-02', 'boss' => 'Victoria', 'team' => [ 'Harry', 'Hillary', 'Howie' ] } ); local $YAML::Indent = 4; local $YAML::UseFold = 1; print Dump( \@foo ); __END__ --- - boss: Clara date: 2005-06-01 team: - Jack - Jill - Joe - boss: Victoria date: 2005-06-02 team: - Harry - Hillary - Howie
      Too bad there isn't an option yet to compress sequences.

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

        > I always start with `perldoc YAML'

        That's what I did too, but the synopsis actually starts with YAML, not with Perl structures.

        grinder and your technique is great for those who are good at thinking in, and writing, Perl data structures. I must shamefully confess that, despite long experience programming Perl, I have trouble distinguishing braces from brackets from curlies, and remembering which does what. So I thought to myself: "Hey, YAML, human-readable! Maybe it can save me from wearing out my AltGr key! (on French keyboard)". But YAML turned out not to be as DWIM as I expected, which isn't surprising. Hence the need for lots of examples for lazy newbies like me.

        Maybe as penance (and when I have time, this was all supposed to be a timesaver...) I'll write up a translation of perldsc into YAML.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://461977]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found