Newbie: yaml array of arrays

Hello,

I’m new to yaml, and yaml in Ruby.

I’m trying to create a yaml file in a text editor, one part of which
will consist of an array of of arrays. The data structure I’m trying
to represent is:

[[‘a’, ‘b’, ‘c’], [‘d’, ‘e’, ‘f’], [‘g’, ‘h’, ‘i’]]

an array of three arrays.

I’ve tried to create this yaml file in a text editor (test.cfg):

– a

  • b
  • c
    – d
  • e
  • f
    – g
  • h
  • i

and read it in as follows

require ‘yaml’
na = open(‘test.cfg’) { |f| YAML.load(f) }

I get a load error:

$ ./test.rb
/usr/lib/ruby/1.8/yaml.rb:119:in load': parse error on line 9, col -1:’ (ArgumentError)
from /usr/lib/ruby/1.8/yaml.rb:119:in load' from ./test.rb:3 from ./test.rb:3:inopen’
from ./test.rb:3

The last line seems o be causing an ArgumentError.

I’m sure my approach is completely off - how does one represent an
array of arrays in yaml, and load it into a Ruby array of arrays?

Thanks.

On 12/09/06, [email protected] [email protected] wrote:

an array of three arrays.

  • h
    /usr/lib/ruby/1.8/yaml.rb:119:in `load’: parse error on line 9, col -1:

Thanks.

You can use IRB to work it out in reverse… start with the ruby array
and generate teh yaml from it.

irb(main):004:0>require ‘yaml’
=>true
irb(main):005:0> puts
[[‘a’,‘b’,‘c’],[‘d’,‘e’,‘f’],[‘g’,‘h’,‘i’]].to_yaml

    • a
    • b
    • c
    • d
    • e
    • f
    • g
    • h
    • i
      =>nil

Farrel

On 06-09-12, at 03:40, [email protected] wrote:

an array of three arrays.

I’ve tried to create this yaml file in a text editor (test.cfg):

I’m sure my approach is completely off - how does one represent an
array of arrays in yaml, and load it into a Ruby array of arrays?

You wanted YAML.load_file not YAML.load; and a yaml file that looks
like this:


    • a
    • b
    • c
    • d
    • e
    • f
    • g
    • h
    • i

Then try:

YAML.load_file ‘your_file.yaml’

[email protected] a écrit :

an array of three arrays.

  • h
    /usr/lib/ruby/1.8/yaml.rb:119:in `load’: parse error on line 9, col -1:

Thanks.

Hello,

YAML.load <<-EOS

    • a
    • b
    • c
    • d
    • e
    • f
    • g
    • h
    • i
      EOS
      => [[“a”, “b”, “c”], [“d”, “e”, “f”], [“g”, “h”, “i”]]

[email protected] wrote:

an array of three arrays.

  • h
    /usr/lib/ruby/1.8/yaml.rb:119:in `load’: parse error on line 9, col -1:

Thanks.

Hi,

This approach should work without trouble. Do this:

puts [%w(a b c), %w(d e f), %w(g h i)].to_yaml

your output should be

    • a
    • b
    • c
    • d
    • e
    • f
    • g
    • h
    • i

Another way is to use the flow-style like this:

  • [a, b, c]
  • {d, e, f]
  • [g, h, i]


Ola B. (http://ola-bini.blogspot.com)
JvYAML, RbYAML, JRuby and Jatha contributor
System Developer, Karolinska Institutet (http://www.ki.se)
OLogix Consulting (http://www.ologix.com)

“Yields falsehood when quined” yields falsehood when quined.

[email protected] wrote:

an array of three arrays.
Or a two-dimensional array.

  • i
    Can I ask what guidance you have that this is valid YAML syntax?
    from /usr/lib/ruby/1.8/yaml.rb:119:in `load'
    from ./test.rb:3
    from ./test.rb:3:in `open'
    from ./test.rb:3

The last line seems o be causing an ArgumentError.

I’m sure my approach is completely off - how does one represent an
array of arrays in yaml, and load it into a Ruby array of arrays?

We are faced here with a question of how one converts a hand-entered,
plain-text file that we hope is valid YAML, into a Ruby array. How shall
we
solve this problem?

Well, we could type a bunch of stuff into a text editor, run it through
a
YAML interpreter, and see if it turns into the desired array when
parsed.
This could take a long time.

But how about this? Why don’t we attack this problem from the other
direction? Why don’t we take the original array, convert it into YAML,
and
see how it looks?

#!/usr/bin/ruby

require ‘yaml’

array = [[‘a’, ‘b’, ‘c’], [‘d’, ‘e’, ‘f’], [‘g’, ‘h’, ‘i’]]

YAML.dump(array,STDOUT)

Output:


    • a
    • b
    • c
    • d
    • e
    • f
    • g
    • h
    • i

Something tells me (even thought I haven’t tried it) that, if I take
this
YAML output and use it as an input, the original array will be
recreated.

One more question. Why are you trying to create YAML data by hand? True,
YAML data is human-readable, but that doesn’t guarantee that it’s
human-writable.

Parting shot. Confucius say, “avoid data formats in which whitespace is
syntactically significant.”

Paul L. wrote:

Parting shot. Confucius say, “avoid data formats in which whitespace is
syntactically significant.”

Whitespace in YAML is only significant when using block style data.


Ola B. (http://ola-bini.blogspot.com)
JvYAML, RbYAML, JRuby and Jatha contributor
System Developer, Karolinska Institutet (http://www.ki.se)
OLogix Consulting (http://www.ologix.com)

“Yields falsehood when quined” yields falsehood when quined.

On 9/12/06, Paul L. [email protected] wrote:

Parting shot. Confucius say, “avoid data formats in which whitespace is
syntactically significant.”

DarnitnowIunderst
andwhyIhavesom
uchtroublewithEn
glishpunctation

-- Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Ola B. wrote:

Paul L. wrote:

Parting shot. Confucius say, “avoid data formats in which whitespace is
syntactically significant.”

Whitespace in YAML is only significant when using block style data.

Well, it was significant in this case – it prevented correct
interpretation
of your entries. Those missing spaces killed the result.

Rick DeNatale wrote:

On 9/12/06, Paul L. [email protected] wrote:

Parting shot. Confucius say, “avoid data formats in which whitespace is
syntactically significant.”

DarnitnowIunderst
andwhyIhavesom
uchtroublewithEn
glishpunctation

Point taken. :slight_smile:


#!/usr/bin/ruby

data=<<EOF
I’ve always thought languages and data formats that rely on whitespace
represent going one meter too far into the realm of making the data look
user-friendly.

EOF

puts data

data.gsub!(/\s+/,“”).gsub!(/(.{1,5})/,"\1 ")

puts data


Output:

I’ve always thought languages and data formats that rely on whitespace
represent going one meter too far into the realm of making the data look
user-friendly.

I’vea lways thoug htlan guage sandd atafo rmats thatr elyon white space
repre sentg oingo nemet ertoo farin tothe realm ofmak ingth edata looku
ser-f riend ly.

(just to undermine my own point.)