I am trying to manipulate an array of arrays, in such a way that I have access to each individual array as a variable.
In my code I am storing 7 different entries of user input in the form of an of an array of arrays. These entries are two values separated by a space i.e. user inputs 1 followed by a space followed by a 4 providing the array ["1","4"]
#user prompt repeated 7 times
puts "enter segments: "
e1 = gets.chomp.split
#etc...
entries = [e1, e2, e3, e4, e5, e6, e7] #e.g. 'eX' may be of form ["n", "n2"]
new_values = []
@dict = [2,3,1,2,3,2,3,3,1]
entries.each do |convert|
convert.each do |get_integers|
get_integers.each do |get_strings|
new_values << @dict[get_strings.to_i]
end
end
end
array_of_new = new_values.slice(2)
I am trying to:
- Convert the elements from each subarray in
entriesto integers - Then in the
entriessubarrays I want to replace each elementiwith@dict[i]
The code I have does all of the above but I end up with a flattened array with all the new values.
How do I break the final array back up into a new array of arrays (so with the same structure as the entries array)? I tried using the slice method, but this is just giving me back the element in the position I am slicing.
Also, I am positive there is a better way to accomplish what I am doing, so if anyone has any advice, I am open to it!
new_values.map { |x| Array(x) }do what you want?