From: kinfe0@... (Kinfe) Date: 2002-01-07T02:36:14+09:00 Subject: [ruby-talk:30355] Re: move index back ward in list.each do {} Yohanes Santoso wrote in message news:<87vgegxlcm.fsf@jenny-gnome.dyndns.org>... > > At this point I would like to advance the next index in a.each but > > remain in b.each, another word, b[4] will not become b[5] after the > > a[had advance], how can I do ? > > I can't really understand your example (including the one in your > latter posting about cows and pigs). But I got what you mean, I think. > > How about this then: > > a=[1,2,3,4,5] > b=[5,4,3,2,1] > next_i = false > a.each do |i| > puts "i=#{i}" > if next_i > next_i = false > next > end > b.each do |j| > if i==j > puts "Advance i to #{i+1}, keep j at #{j}" > i += 1 > next_i = true > redo > end > puts "i=#{i}\tj=#{j}" > end > end > > The output for the above follows after my signature. > > In all honesty, the above is ugly and confusing. How about using > nested while? That will allow a much finer control, shorter code, and > makes a clearer point because, at least for me, if I see a nested > each, I do not expect for the inner each to control the outer > each. > > YS. > > i=1 > i=1 j=5 > i=1 j=4 > i=1 j=3 > i=1 j=2 > Advance i to 2, keep j at 1 > i=2 j=1 > i=2 > i=3 > i=3 j=5 > i=3 j=4 > Advance i to 4, keep j at 3 > i=4 j=3 > i=4 j=2 > i=4 j=1 > i=4 > i=5 > Advance i to 6, keep j at 5 > i=6 j=5 > i=6 j=4 > i=6 j=3 > i=6 j=2 > i=6 j=1 This may be too much for me at this point of learning Ruby. Anyway, the data isn't all numeric, they are lines of records reading into array cow, pig. format is type weight date origin cow 100 010502 kansas cow 300 010102 ohio cow 200 010101 newyork more pig 600 010101 somewhere pig 100 010101 mexico pig 400 010102 virginia pig 500 010202 california more many more of these with different weight, my real goal is to match the weight pair until they are equal, so if the pig's weight is 600 pounds, and the cow is 100 then I want to subtract pig_remain = pig(600) - cow(100) , so I will want to keep reading in the cow loop but not the pig loop and repeat the process of subtraction until the weight is equal . This is of I have done. cow.each do |c| a=cow.split pig.each do |p| b=split.pig if (b[1] >= a[1]) pig_remain = b[1] - a[1] # #and get out of here to cow loop to fetch another record and do the #subtraction until they equal before fetching another pig.each |p| # # If I could move the pig.each |p| back one position then it should give me # what I want, right? break end end end