From: "Kroeger, Simon (ext)" Date: 2005-12-05T23:13:46+09:00 Subject: Re: increasing counter whithin loop? perhaps: a.inject(false) do |skip, elt| puts elt if !skip elt=="b" end (no, I don't think this is realy nice) cheers Simon > -----Original Message----- > From: Patrick Gundlach [mailto:clr9.10.randomuser@spamgourmet.com] > Sent: Monday, December 05, 2005 3:08 PM > To: ruby-talk ML > Subject: increasing counter whithin loop? > > 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 > >