Forum: Ruby array of arrays

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B49a509b276713da7c4fa32c4f209692?d=identicon&s=25 Di Bnath (divya)
on 2007-07-20 18:37
i have a database with some 10 records each containing 2 columns,
sno,name,city.

I want to push this into an array of arrays.
i see that Ruy doesnt support multi-dimensional arrays.
Any ideas of how to do it?
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-07-20 18:43
(Received via mailing list)
Hi --

On Sat, 21 Jul 2007, Divya Badrinath wrote:

> i have a database with some 10 records each containing 2 columns,
> sno,name,city.
>
> I want to push this into an array of arrays.
> i see that Ruy doesnt support multi-dimensional arrays.
> Any ideas of how to do it?

Just use arrays as array elements:

[ [1,2,3], [4,5,6], [7,8,9] ]

for example.


David
B49a509b276713da7c4fa32c4f209692?d=identicon&s=25 Di Bnath (divya)
on 2007-07-20 22:26
unknown wrote:
> Hi --
>
> On Sat, 21 Jul 2007, Divya Badrinath wrote:
>
>> i have a database with some 10 records each containing 2 columns,
>> sno,name,city.
>>
>> I want to push this into an array of arrays.
>> i see that Ruy doesnt support multi-dimensional arrays.
>> Any ideas of how to do it?
>
> Just use arrays as array elements:
>
> [ [1,2,3], [4,5,6], [7,8,9] ]
>
> for example.
>
>
> David

Thank you.

I will try it.
B49a509b276713da7c4fa32c4f209692?d=identicon&s=25 Di Bnath (divya)
on 2007-07-25 23:44
I didnt get it.
I am new to Ruby.

i want it to be like this.
row = { " "a","b","c" ",
        " "d","e","f" ",
        " "g","h","i" "}
sow that
row[0] gives "a","b","c"
and row[1] gives "d","e","f"

say if
col is an array and
col = "a","b","c"

can i do
row.push(col)
?
Aaa2b1f12b65d33422e8cdc48d70c0f9?d=identicon&s=25 Stephen Ball (Guest)
on 2007-07-26 00:46
(Received via mailing list)
Try this,

irb(main):001:0> row = [["a","b","c"],["d","e","f"],["g","h","i"]]
=> [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
irb(main):002:0> row[0]
=> ["a", "b", "c"]
irb(main):003:0> row[1]
=> ["d", "e", "f"]

For your push operation you can use <<, as in:

irb(main):004:0> addition = ["j","k","l"]
=> ["j", "k", "l"]
irb(main):005:0> row << addition
=> [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"], ["j", "k", "l"]]
This topic is locked and can not be replied to.