I have two nested arrays:
array1 = [[String_1, [String_2, [String_3.1, String_3.2, …]…]…] String_4…]
array2 = [[String_3.1, [String_3.1.1, String_3.1.2]], [String_3.2, [String_3.2.1, String_3.2.2]]]
I need to replace String_3.1 in array1 with the matching part of array2:
[String_3.1, [String_3.1.1, String_3.1.2]]
This whole part should slip into the position of String_3.1 in array1.
I can't access the values to exchange by the find_index method because they are buried in the structure. I cannot flatten the array because the structure needs to stay intact.
I tried an approach with a recursive function:
def find_insert_point(array1)
array1.each do |value|
if value.is_a?(Array) == true
find_insert_point(value)
puts "#{value}" if value.class == String
end
end
end
This iterates through the entries without flattening the array. But I cannot use map! to alter the underlying real array1 structure while iterating over array2 data (not included in the code).
Is there any way to search for a specific index (e.g. by string) in a nested array with multiple levels and exchange a hit with other data (e.g. a sub-array)?
String_3.1, etc. are invalid. They must be something else.1is an invalid method name.array1andarrayvalid arrays of strings, and show your desired output, so that readers who give answers can better show how the output is obtained.