[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: Re: increasing counter whithin loop?
From: Neil Stevens <neil () hakubi ! us>
Date: 2005-12-05 15:17:34
Message-ID: HPYkf.594$EB3.38 () trnddc09
[Download RAW message or body]
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?
array = [1, :skip, :hidden, 2, :skip, :hidden, 3, :skip]
# Zipped to ensure i is uniquely index()ed
# Also makes sure use array non-destructively
zip = array.zip
for i in zip
if i[0] == :skip
zip.delete_at(zip.index(i) + 1)
else
puts i[0]
end
end
Output:
1
2
3
--
Neil Stevens - neil@hakubi.us
'A republic, if you can keep it.' -- Benjamin Franklin
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic