[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

YAML no reorder data at loading

Ranieri Teixeira

3/4/2008 5:33:00 AM

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

1 Answer

Stefano Crocco

3/4/2008 7:49:00 AM

0

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