From: Safas Khkjh on
Hi,
I'm very new to Ruby (2 days), I'm mostly used to Python/PHP/C++.

What would be ideal would be a multidimensional array however from what
I have read Ruby does not support multidimensional arrays.

I want to be able to store data within multiple arrays and have the
values in all of the arrays linked together.

Example;

Array1
[vegetable]
[fruit]

Array2
[carrot]
[banana]

Array3
[tomato]
[apple]

I want to be able to some how output;

Vegetables;
carrot, tomato

Fruit;
banana, apple

With a multidimensional array this would be trivial;

[vegetable][carrot]
[vegetable][tomato]
[fruit][banana]
[fruit][apple]

Any help much appreciated,
Thank you.
--
Posted via http://www.ruby-forum.com/.

From: Rein Henrichs on
On 2010-06-10 14:02:40 -0700, Safas Khkjh said:

> Hi,
> I'm very new to Ruby (2 days), I'm mostly used to Python/PHP/C++.
>
> What would be ideal would be a multidimensional array however from what
> I have read Ruby does not support multidimensional arrays.
>
> I want to be able to store data within multiple arrays and have the
> values in all of the arrays linked together.
>
> Example;
>
> Array1
> [vegetable]
> [fruit]
>
> Array2
> [carrot]
> [banana]
>
> Array3
> [tomato]
> [apple]
>
> I want to be able to some how output;
>
> Vegetables;
> carrot, tomato
>
> Fruit;
> banana, apple
>
> With a multidimensional array this would be trivial;
>
> [vegetable][carrot]
> [vegetable][tomato]
> [fruit][banana]
> [fruit][apple]
>
> Any help much appreciated,
> Thank you.

In general, multidimensional arrays are just arrays that contain arrays:

[[1,1,1],
[2,2,2],
[3,3,3]]

However, a Hash seems more appropriate for your specific use case:

{"vegetable" => ["carrot", "tomato"],
"fruit => ["banana", "apple"]}

--
Rein Henrichs
http://puppetlabs.com
http://reinh.com