Skip index in array.each_with_index loop

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

Use the next statement.

http://www.tutorialspoint.com/ruby/ruby_loops.htm

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

select

reject

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,

use next if (i % 10 != 0)

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.

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

You 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]]

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