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

YAML newbie help

by Tortue (Scribe)
on May 31, 2005 at 09:14 UTC ( #461966=perlquestion: print w/replies, xml ) Need Help??
Tortue has asked for the wisdom of the Perl Monks concerning the following question:

O Wise Ones, I've always though YAML was cool. But today I tried to actually use it for a quickie, and I realized it's not quite so easy. I'm looking for some documentation that can get me started real quick, with cut-and-pasteable practical examples. Please help me get past this kind of nonsense:
--- !perl/YAML::Error code: YAML_PARSE_ERR_NO_SEPARATOR msg: Expected separator '---' line: 4 document: 2 ... at line 20
At the main YAML site, I've seen the spec, the reference card, the Get Started, the faq. I've seen the module documentation. I've seen this nice article presenting YAML. I've searched this site. But it's all too advanced or too allusive for what I need.

Replies are listed 'Best First'.
Re: YAML newbie help
by grinder (Bishop) on May 31, 2005 at 09:46 UTC
    I'm looking for some documentation that can get me started real quick, with cut-and-pasteable practical examples.

    I've always found that the easiest way to write code to emit valid YAML is to create the target structure in Perl and dump it out, and look at the result.

    #! /usr/local/bin/perl -w use strict; use YAML; my $r = [ 1, 2, { a => [2, 3], b => [4, 5, 6] }, { c => { d => 'd', e => 'e', f => [6, 7, 8] }, d => [10, 20, 3 +0] }, ]; print YAML::Dump($r), "\n";

    Which produces

    --- - 1 - 2 - a: - 2 - 3 b: - 4 - 5 - 6 - c: d: d e: e f: - 6 - 7 - 8 d: - 10 - 20 - 30
    And from there it's pretty easy to whip up some code that produces this target output. From these sorts of empirical observations you can usually figure out what YAML you need to write to achieve the data structure you want to play with.

    - another intruder with the mooring in the heart of the Perl

Re: YAML newbie help
by zentara (Archbishop) on May 31, 2005 at 10:40 UTC
    Here is a newbie tip:

    The only thing I've ever needed to remember when using YAML is the syntax for dumping with better output.

    #instead of print Dump( %hash ),"\n"; #do print Dump( [ \%hash ] ), "\n";

    I'm not really a human, but I play one on earth. flash japh
Re: YAML newbie help
by PodMaster (Abbot) on May 31, 2005 at 09:26 UTC
    I'm looking for some documentation that can get me started real quick, with cut-and-pasteable practical examples
    Everything you've references, the yaml site ... all fit the bill.
    Please help me get past this kind of nonsense...
    Why is that nonsense? It says you're missing '---' at some place, what exactly are you doing (where is the relevant code, sample data -- How (Not) To Ask A Question )?

    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.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://461966]
Approved by Mutant
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2017-12-16 23:00 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (459 votes). Check out past polls.