4

I have an array (outside array) that contains three arrays (inside arrays), each of which have three elements.

array = [[a, b, c], [d, e, f], [g, h, i]]

I want to select the specific inside array using an index of the outside array and then select the value within the selected inside array based off its index. Here is what I tried:

array.each_index{|i| puts "letter: #{array[i[3]]} " } 

I was hoping that would give me the following output

letter: c letter: f letter: i

but instead, I get

letter: [[a, b, c], [d, e, f], [g, h, i]]

I also tried

array.each_index{|i| puts "letter: #{array[i][3]} " } 

but I get the same result. Please any suggestions are very appreciated. I need a simple explanation.

5 Answers 5

8

each_index is an Enumerable which goes through all indices and performs an action on each one. When it's done it will return your original collection as it's not its job to change it. If you want to output stuff on the screen via puts / print then each_index is fine.

If you want to create a new collection as a result of going through all the elements of an original collection, you should use map.

e.g.

array = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
new_array = array.map {|el| el[2]}
=> ["c", "f", "i"]

array.map iterates through array's elements so in every step |el| is an element, not an index, as in: ['a', 'b', 'c'] in the first iteration, ['d', 'e', 'f'] in the second one and so on...

Just pointing this out since I don't know what's the goal of what you're trying to do.

Sign up to request clarification or add additional context in comments.

Comments

5

do it like this:

array.each_index{|i| puts "letter: #{array[i][2]} " } 

Since you want letter at index 2, not 3.

Also array should be defined like this:

array = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

1 Comment

I actually meant to put 2 not 3 that was typo on my part... my actual code I am working on has 52 arrays within the main array (its a deck of cards, so index 0-51). I was hasty in trying to simplify it for this post.
3

You could use map like so:

a = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
a.map(&:last)
# => ["c", "f", "i"]

Or if you really want the puts and not the collected values:

a.each {|v| puts "letter: #{v.last}"}

You could also use Ruby's Matrix class if there's more Matrix-y stuff you want to do:

require 'matrix'
m = Matrix[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
last_column = m.column_vectors.last
# => Vector["c", "f", "i"]

You can now use any of the Vector methods on last_column, including to_a, which will put you back in familiar territory with the last column's values.

Comments

0

Arrays in ruby are indexed from 0, not from 1. So:

array.each_index{|i| puts "letter: #{array[i][2]} " }

should give you what you want.

1 Comment

again that was a typo on my part my actual code has indexes 0-51 so I just got a bit hasty in writing out my question. What your writing there is what I initially thought would work, I mean it makes sense but for some reason its just not working.
0

You could try the below also:

p RUBY_VERSION

arr = [[1,2,3],[4,5,6],[11,12,13]]

arr.each{|x| p x; x.each_index{|i| p "Digit :: #{x[i]}" if i == 2} }

output:

"2.0.0"
[1, 2, 3]
"Digit :: 3"
[4, 5, 6]
"Digit :: 6"
[11, 12, 13]
"Digit :: 13

1 Comment

ahh this is pretty much exactly what I was looking for, I don't know why but I didn't think to nest the loops. Makes sense though, you have a nested array so why not nest the loops... good stuff. Although I like the other suggestions too because at some point I will need to modify the array and not just print it. But this answers my initial questions. Thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.