array of arrays

D

Divya Badrinath

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?
 
D

dblack

Hi --

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

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
D

Divya Badrinath

unknown said:
Hi --

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

Divya Badrinath

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)
?
 
S

Stephen Ball

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"]]
 

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
474,263
Messages
2,571,312
Members
47,987
Latest member
Gaurav

Latest Threads

Top