ruby - how can you loop through multiple arrays parallel? -
I have 4 arrays I ["one", "two", "three"] ["1", "2 "3" ["un", "deux", "trois"] ["
Is it possible to add each element to its elements?
Ends with single lines like
"one, 1, un, ichi" \ n "two, 2, deux, ni" \ n
< P> and so on. Is it possible to do this in a loop?
in array1 [i] + for me (1..rayray1.count) "," + Array2 inserts <+> unexpected number of ARAR What happens when & amp; they are each uneven size ?
OK if you know that they were all the same length:
(0 ... array1.length) .iE {| i | puts} Edit the array 1 [i] + "," + 2 [i] + +, "+ array 3 [I] +", + + array4 [i]}
The following code works
array1 = ["one", "two", "three"] array2 = ["1", "2", "3 "] Array3 = [" un "," deux "," trois "] array4 = [" ichi "," ni "," san "] (0 ... array1.length) .each {| I | Edit Array1 [i] + "," + array2 [i] + "," + array3 [i] + ", + + array4 [i]}
Edit 2: Say what happens if you do not know how many arrays will be?
I will suggest creating an array of arrays; Create an array array array (essentially a 2D array but can not be indexed like this one) and with each row is printed for each array in the array list.
This code works:
array1 = ["one", "two", "three"] array 2 = ["1", "2", "3"] array 3 = ["un", "deux", "trois array4 = [" ichi "," ni "," san "] arrayList = [] arrayList.push (array1, array2, array3, array4) p array (0 ... array1.length) .each {| i | (0 ... arrayList.length). Each {| j | print array list [ja] [i] + ","} print "\ n"} < / Code>
Comments
Post a Comment