How does one tell YAML to use an array substructure rather than a hash?
Given the following YAML data:
an:
- at sf
- fc di
- ps:
- na vb
- ex anEx
- ps:
- na no
- ex otherEx
- pr pro
in:
- at sf
- fc lo
YAML gives it the structure: {[{[...]}]}, that is, Hash embeds Array
which embeds another Hash which embeds Array, as below (output
reformatted for clarity).
I feel that the structure below: {[[[...]]]}, that is, hash embeds
array which embeds another array makes it easier to pick off keys and
values than the other structure (though, honestly, I haven't gone that
far yet).
This doesn't look like a hash containing an array to me. The "ps" entries
inside look like they're hashes, the key is "ps", the value is an array
containing ["na vb", "ex anEx"]...
In any event, I think if you have value: stuff in YAML, it will decide it's a
hash entry. Array entries start with '-'.
I feel that the structure below: {[[[...]]]}, that is, hash embeds
array which embeds another array makes it easier to pick off keys and
values than the other structure (though, honestly, I haven't gone that
far yet).
I think I can handle picking up keys and values here, so I'm happy with
the nesting. But now my problem is how to add extra levels of nesting
to the YAML statements, where "see" has its own substructure, something
like:
ArgumentError: parse error on line 9, col 7: ` sea:
'
from c:/ruby/lib/ruby/1.8/YAML.rb:119:in `load'
from c:/ruby/lib/ruby/1.8/YAML.rb:119:in `load'
from (irb):52
My apologies for the incorrect OP. And thanks.
basi
I think I can handle picking up keys and values here, so I'm happy with
the nesting. But now my problem is how to add extra levels of nesting
to the YAML statements, where "see" has its own substructure, something
like:
Did you mean "sea" has it's own substructure, i.e. the hash key "cc" points to
an array with two values "see" and another hash containing one entry where
the key is "sea" and the value is an array containing "weed" and "sick".
If that's your goal, you were close, all you needed was the line with the dash
on it before "sea:"
Btw, the way I figured this out was by creating the structure in ruby in IRB,
then calling "puts h.to_yaml". There are lots of braces and brackets to work
through, but the error messages tend to be better.
Jamal,
Yes, that'd work for this example. In my project, I would not know
beforehand what the array index would be. I suppose one could extract
the array index from its value.
But, after a good night's sleep, I found the structure that looks like
will work for me: hash all the way down.