YAML - Time to give?


#1

Guys-
I am trying to create an YAML file like the following:
3
— Begin
ga:
name: Georgia
capital: savanah
population: 1 mil
cities:
city:
name: Atlanta
zip: 30328
city:
name: Savanah
zip: 30304
tx:
name: Texas
capital: Dallas
population: 2 mil
cities:
city:
name: Fortworth
zip: 22222
city:
name: Dallas
zip: 12345
#—End

I keep getting “parse” error while loading it to yaml. Even if I manage
to load with “-”, I could never fetch the City and zip details. Can
anyone help me with this nested yaml file? thanks


#2

Hi –

On Sun, 15 Jan 2006, thila thila wrote:

    name: Atlanta
    zip: 30328
 city:
    name: Savanah
    zip: 30304

One problem is that you’ve just redefined the “city” key for this
hash. So you’ll end up with Savannah but no Atlanta.

I keep getting “parse” error while loading it to yaml. Even if I manage
to load with “-”, I could never fetch the City and zip details. Can
anyone help me with this nested yaml file? thanks

When I cut-and-pasted your text and did a YAML.load on it, it worked
fine (other than the logic error with the “city” key). Do you have a
tab character in the file somewhere, instead of leading spaces?

Try this and see if it prints 30328:

str = <<EOM
ga:
name: Georgia
capital: Savannah
population: 1 mil
cities:
Atlanta:
zip: 30328
Savannah:
zip: 30304
EOM
require ‘yaml’
info = YAML.load(str)
p info[“ga”][“cities”][“Atlanta”][“zip”]

David


David A. Black
removed_email_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!


#3

Thanks dave. I believe I had tab spaces, which caused load error -
correct me if I am wrong. You suggestion works. However, I wrote a
program that outputs the yaml file with hardcoded values. Then I edited
the output files to match my needs - works beautifully.

Thanks