YAML no reorder data at loading

  • Thread starter Ranieri Teixeira
  • Start date
R

Ranieri Teixeira

Hi,
I want to load the following yaml content:

Yellow: 1
Blue: 2
Red: 3

And build two arrays: one for keys, that must be in this order:

['Yellow', 'Blue', 'Red'] and another for values:

[1, 2, 3]

How can I do that?

Thanks

--=20
Ranieri Barros Teixeira
Ci=EAncia da Computa=E7=E3o - Faculdade de Computa=E7=E3o - Universidade F=
ederal do
Par=E1 (UFPA)
http://rubyxchart.rubyforge.org
 
S

Stefano Crocco

Alle Tuesday 04 March 2008, Ranieri Teixeira ha scritto:
Hi,
I want to load the following yaml content:

Yellow: 1
Blue: 2
Red: 3

And build two arrays: one for keys, that must be in this order:

['Yellow', 'Blue', 'Red'] and another for values:

[1, 2, 3]

How can I do that?

Thanks

As far as I can tell, you can't. This is because the notation

key: value

in YAML represents a hash, which is sorted in an arbitrary order. If you want
to keep the order, the simplest way is to replace the YAML hash with a nested
list:

- [Yellow, 1]
- [Blue, 2]
- [Red, 3]

I hope this helps

Stefano
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top