Can anyone help me on how to skip index in ARRAY.each_with_index loop? Please look at below case: In a ARRAY.each_with_index loop, I would like increase the index by 10 whenever a condition is met. arr is array of numbers from 0 to 50 arr.each_with_index do |x,i| if i % 10 == 0 puts "#{x} at #{i}" i = i+10 //index increased by 10 end end The output should be: 10 at 10 30 at 30 50 at 50 Thanks Ajit -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d060dba071d03754b6d3f816898091eb%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Use the `next` statement. http://www.tutorialspoint.com/ruby/ruby_loops.htm -- Dheeraj Kumar On Thursday 23 May 2013 at 2:54 PM, Ajit Teli wrote:> Can anyone help me on how to skip index in ARRAY.each_with_index loop? > > Please look at below case: > > In a ARRAY.each_with_index loop, I would like increase the index by 10 > whenever a condition is met. > > arr is array of numbers from 0 to 50 > arr.each_with_index do |x,i| > if i % 10 == 0 > puts "#{x} at #{i}" > i = i+10 //index increased by 10 > end > end > > The output should be: > 10 at 10 > 30 at 30 > 50 at 50 > > Thanks > Ajit > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d060dba071d03754b6d3f816898091eb%40ruby-forum.com?hl=en-US. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/C9D341988142443CAD90D906536A9EB6%40gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
On Thursday, 23 May 2013 11:24:36 UTC+2, Ruby-Forum.com User wrote:> > Can anyone help me on how to skip index in ARRAY.each_with_index loop? > > Please look at below case: > > In a ARRAY.each_with_index loop, I would like increase the index by 10 > whenever a condition is met. > > arr is array of numbers from 0 to 50 > arr.each_with_index do |x,i| > if i % 10 == 0 > puts "#{x} at #{i}" > i = i+10 //index increased by 10 > end > end > > The output should be: > 10 at 10 > 30 at 30 > 50 at 50 > > Thanks > Ajit > > Could you explain it in a more clear way, "whenever a condition is met".If you just want to select the elements that meet some condition, use one of the following Array methods: keep_if <http://ruby-doc.org/core-2.0/Array.html#method-i-keep_if> select <http://ruby-doc.org/core-2.0/Array.html#method-i-select> reject <http://ruby-doc.org/core-2.0/Array.html#method-i-reject>> -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f9ddb68c-86b8-4cc1-a0da-9b555a594880%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Dear Dheeraj, Thanks for your reply. The statement `next` will increment the index by 1. But I want to increment the index by 10. Thanks, -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/325175e1a58db9cfa3c441edb6bb252e%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
use `next if (i % 10 != 0)` -- Dheeraj Kumar On Thursday 23 May 2013 at 3:25 PM, Ajit Teli wrote:> Dear Dheeraj, > > Thanks for your reply. > > The statement `next` will increment the index by 1. But I want to > increment the index by 10. > > Thanks, > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/325175e1a58db9cfa3c441edb6bb252e%40ruby-forum.com?hl=en-US. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/A8D33CB871B241E29949158562D87EE8%40gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Write a loop that generates the desired indexes and then reference arr: (0..5).each do |j| i = 10*j puts "#{arr[i]} at #{i}" end Your example output does not match the code. It does not include 0, 20, and 40. On Thursday, May 23, 2013 5:24:36 AM UTC-4, Ruby-Forum.com User wrote:> > Can anyone help me on how to skip index in ARRAY.each_with_index loop? > > Please look at below case: > > In a ARRAY.each_with_index loop, I would like increase the index by 10 > whenever a condition is met. > > arr is array of numbers from 0 to 50 > arr.each_with_index do |x,i| > if i % 10 == 0 > puts "#{x} at #{i}" > i = i+10 //index increased by 10 > end > end > > The output should be: > 10 at 10 > 30 at 30 > 50 at 50 > > Thanks > Ajit > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6cb2b3e6-32c3-4f5d-b85a-fdfa1c5427f4%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
jsnark wrote in post #1109941:> Write a loop that generates the desired indexes and then reference arr: > > (0..5).each do |j| > i = 10*j > puts "#{arr[i]} at #{i}" > end > > Your example output does not match the code. It does not include 0, 20, > and 40.# Print every 10th object (0..50).each_slice(10) { |slice| p slice[0] } 0 10 20 30 40 50 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6ac2266efa2580437413cfc45d8d31c8%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
On 2013-May-23, at 05:24 , Ajit Teli wrote:> Can anyone help me on how to skip index in ARRAY.each_with_index loop? > > Please look at below case: > > In a ARRAY.each_with_index loop, I would like increase the index by 10 > whenever a condition is met. > > arr is array of numbers from 0 to 50 > arr.each_with_index do |x,i| > if i % 10 == 0 > puts "#{x} at #{i}" > i = i+10 //index increased by 10 > end > end > > The output should be: > 10 at 10 > 30 at 30 > 50 at 50 > > Thanks > AjitYou need to think about what you really need to do, look at the options to use select or reject/delete_if, or take the time to understand what next does. Of course, you don''t seem to have even tried to run your code since your output lacks "0 at 0". -Rob For example: irb2.0.0> arr = (0..50).to_a #2.0.0 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] irb2.0.0> arr.each_with_index do |element,index| ?> next unless index % 10 == 0 irb2.0.0> puts "#{element} at #{index}" irb2.0.0> end 0 at 0 10 at 10 20 at 20 30 at 30 40 at 40 50 at 50 #2.0.0 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] irb2.0.0> arr.each.with_index.select {|element,index| index % 10 == 0}.each do |element,index| ?> puts "#{element} at #{index}" irb2.0.0> end 0 at 0 10 at 10 20 at 20 30 at 30 40 at 40 50 at 50 #2.0.0 => [[0, 0], [10, 10], [20, 20], [30, 30], [40, 40], [50, 50]] -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4FA0239B-28B5-4432-B4DC-E578C5C50BB5%40agileconsultingllc.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
On 23 May 2013 13:52, jsnark <swr-I/mPKdbEGRBWk0Htik3J/w@public.gmane.org> wrote:> Write a loop that generates the desired indexes and then reference arr: > > (0..5).each do |j| > i = 10*j > puts "#{arr[i]} at #{i}" > end > > Your example output does not match the code. It does not include 0, 20, and > 40.I don''t think the OP wanted 20 and 40 which is why he is adding 10 rather than 9. I suspect the example is just an example and does not represent the actual code he wants, in particular there is likely to be an else block, and it that which he wishes to skip. I think the answer to his question, however, is that there is no way to change the index within the block in order to skip elements, so he will have to use one of the suggestions made by others. Colin> > > On Thursday, May 23, 2013 5:24:36 AM UTC-4, Ruby-Forum.com User wrote: >> >> Can anyone help me on how to skip index in ARRAY.each_with_index loop? >> >> Please look at below case: >> >> In a ARRAY.each_with_index loop, I would like increase the index by 10 >> whenever a condition is met. >> >> arr is array of numbers from 0 to 50 >> arr.each_with_index do |x,i| >> if i % 10 == 0 >> puts "#{x} at #{i}" >> i = i+10 //index increased by 10 >> end >> end >> >> The output should be: >> 10 at 10 >> 30 at 30 >> 50 at 50 >> >> Thanks >> Ajit >> >> -- >> Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/6cb2b3e6-32c3-4f5d-b85a-fdfa1c5427f4%40googlegroups.com?hl=en-US. > > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvR8rUqPfaamjtW%3DQy9T2TXdQMvEO44Y_JE31rbpsq0HQ%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.