From: Martin DeMello Date: 2005-12-07T18:47:34+09:00 Subject: Re: increasing counter whithin loop? Patrick Gundlach wrote: > Hello again, > > > thanks for the answers. I have been very unclear what I wanted, but > Malte, Guy and JEGII seemed to have read my mind. > > a=%w( a b b d b e ) > > a.each_with_index do |x, i| > next if x == "b" and a[i - 1] == "b" > if x=="b" and a[i+1]=="b" > puts "double b" > else > puts x > end > end I'd use Gary Watson's method, and cache the last seen element, rather than rely upon the index (e.g. if you use a stream rather than an array, you no longer have access to a[i-i]). martin