From: Gary Watson Date: 2005-12-06T00:57:33+09:00 Subject: Re: increasing counter whithin loop? a = ["a", "b", "c", "d", "e"] temp = nil p a.select {|x| if temp == "b" then temp = nil; next end; temp = x;true} outputs ["a", "b", "d", "e"] Probably could be cleaner. Patrick Gundlach wrote: > Hi, > > a very basic question... > > I'd like to output the sequence "a b d e", by testing if the current > element is == "b" then skip the next element and continue the loop. The > obvious solution doesn't look rubyish to me, how could I use the first > or second attempt to get the desired solution? > > Patrick > -------------------------------------------------- > a=%w( a b c d e ) > > # incorrect, outputs "a b c d e" > 0.upto(a.size - 1) do |i| > puts a[i] > if a[i]=="b" > # skip next element > # but i won't get affected > i += 1 > end > end > > # incorrect, outputs "a b c d e" > for i in 0...a.size > puts a[i] > if a[i]=="b" > # skip next element > # but i won't get affected > i += 1 > end > end > > # incorrect, outputs nothing... is there a next_next ? > a.each do |elt| > puts elt > if elt=="b" > # skip next element > # ?? > end > end > > # this one works, but is ugly > i=0 > while i < a.size > puts a[i] > if a[i]=="b" > # skip next element > i += 1 > end > i += 1 > end