I have 3 arrays of equal length. Some spots are nil, which complicates things, but I need to retain their order.
a = [5.2, 3.0, 1.21, 7.0, 5.0, 5.0, 6.0, 8.0, 10.0, 10.0]
b = [nil, nil, [{"price"=>1.99, "size"=>269.897475661239}], nil, nil, nil, nil, nil, nil, nil]
x = [6.0, 6.2, 2.5, 5.0, 9.0, 2.36, 15.5, 20.0, nil, nil]
(Step One, I want to iterate over b so that b = [nil, nil, 1.99, nil, nil, nil, nil, nil, nil, nil]. Just need ["price"], ignore ["size"]. Couldn't figure that out.)
Step Two, I want to create a new array (c) that averages a and b but where there is nil, just take the one that has a value. In other words, c would = [5.2, 3.0, 1.6, 7.0, 5.0, 5.0, 6.0, 8.0, 10.0, 10.0] which looks like a except the third spot the average of 1.21 and 1.99 (1.6).
So I have my original third array x = [6.0, 6.2, 2.5, 5.0, 9.0, 2.36, 15.5, 20.0, nil, nil]. Step Three, I want to compare c and x to and create a new array z that takes the SMALLER* of the two numbers, or if nil, the one that has a value. z is the result I would like.
Thus z should = [6.0, 6.2, 2.5, 7.0, 9.0, 5.0, 15.5, 20.0, 10.0, 10.0] (if my eyes are correct). (*Edit: I meant the larger of the two numbers, which is why this array doesn't match below answer, so I used that answer below but used .max instead of .min )
I know those steps are tedious, but I need to go in that order because I have lots of arrays where I need to average 2 then compare with a third and take the larger number and with random nil values laced throughout, it gets beyond my abilities. Can't figure it out, and would greatly appreciate some help! Thank you!
a,bandxalways the same length? 2. Canacontain anynilelements? 3. Do you want the arrays produced by "Step One" and "Step Two", or just the one produced by "Step 3", considering that the "Step 3" array could be constructed without first constructing the other two arrays.bcontain integers as well asniland an array containing a hash?nilor an array containing a hash as displayed. great follow-up questions, thank you for your excellent answer below!