# Question about array of array

Hey guys, I’m having a problem regarding array of array

I have this array :

matriz = [
[
[ [“Armando”, “P”], [“Dave”, “S”] ],
[ [“Richard”, “R”], [“Michael”, “S”] ],
],
[
[ [“Allen”, “S”], [“Omer”, “P”] ],
[ [“David E.”, “R”], [“Richard X.”, “P”] ]
]
]

How can I loop into it in order to get every value of each position?

e.g: [Armando,“p”] - [“Dave”,“S”] - “Richard”,“R”

When I do a matriz[0][0] why in the input show like this: ?

Armando
P

How can I keep the formatation ?

Thank you guys,

Hi,

If you write matriz[0][0], you will get the array

[ [“Armando”, “P”], [“Dave”, “S”] ]

That’s because matriz[0] yields the first entry of matriz, which is

[
[ [“Armando”, “P”], [“Dave”, “S”] ],
[ [“Richard”, “R”], [“Michael”, “S”] ],
]

And matriz[0][0] yields the first entry of this array, which is

[ [“Armando”, “P”], [“Dave”, “S”] ]

I’m not sure if that is what you wanted? If you want the matrix to
consist of name entries like [“Armando”, “P”], you will have to change
its structure. This code, for example, generates a matrix with 4 rows
and 2 columns:

my_matrix = [

# row 0

[
# column 0
[“Armando”, “P”],
# column 1
[“Dave”, “S”]
],

# row 1

[
# column 0
[“Richard”, “R”],
# column 1
[“Michael”, “S”]
],

[
# column 0
[“Allen”, “S”],
# column 1
[“Omer”, “P”]
],

# row 3

[
# column 0
[“David E.”, “R”],
# column 1
[“Richard X.”, “P”]
]
]

By my_matrix[0][0] you will now get the name array

[“Armando”, “P”]

You can check this by writing

p my_matrix[0][0]

Don’t use the “puts” method in this case. It will print out every entry
of the array line by line instead of showing the array itself.

To iterate over every matrix element, you can write

my_matrix.each do |row|
row.each do |entry|
p entry
end
end

my_matrix.each iterates over the entries of my_matrix, which are the
rows of the matrix. And then you iterate over the entries of the rows,
which are the elements of the matrix.

By the way: Ruby has a built-in class for matrices. That’s probably what
you are looking for.

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/matrix/rdoc/

Jacques