0

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 entries to integers
  • Then in the entries subarrays I want to replace each element i with @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!

1
  • Does something like new_values.map { |x| Array(x) } do what you want? Commented Jan 25, 2016 at 22:57

1 Answer 1

1

Try something like this:

entries = []
7.times do 
    puts "enter segments: "
    entries << gets.chomp.split.map!{|e| e.to_i }
end

@dict = [2,3,1,2,3,2,3,3,1]
array_of_new = []

entries.each do |sub_array|
    s_arr = []
    sub_array.each do |element| 
        s_arr << @dict[element]
    end
    array_of_new << s_arr
end

p array_of_new

If you wanted to use the each_slice method then you could do something like this:

entries = []
7.times do 
    puts "enter segments: "
    entries << gets.chomp.split.map!{|e| e.to_i }
end


@dict = [2,3,1,2,3,2,3,3,1]
arr = []

entries.each do |sub_array|
    sub_array.each do |element| 
        arr << @dict[element]
    end
end

array_of_new = []
arr.each_slice(2) {|s| array_of_new << s }                          

p array_of_new

Update: An even shorter and smarter way would be to actually transform the entries array instead of having to create new container arrays:

entries = []
7.times do 
    puts "enter segments: "
    entries << gets.chomp.split.map!{|e| e.to_i }
end

@dict = [2,3,1,2,3,2,3,3,1]

entries.each do |sub_array|
    sub_array.map!{|e| @dict[e] }
end

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

2 Comments

This is exceptional. Thank you!
@Jbur43 Glad I could help. Just added a third way which you may find useful too.

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.