Skip index in array.each_with_index loop

187 views
Skip to first unread message

Ajit Teli

unread,
May 23, 2013, 5:24:36 AM5/23/13
to rubyonra...@googlegroups.com
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/.

Dheeraj Kumar

unread,
May 23, 2013, 5:32:53 AM5/23/13
to rubyonra...@googlegroups.com

-- 
Dheeraj Kumar

--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Javix

unread,
May 23, 2013, 5:39:44 AM5/23/13
to rubyonra...@googlegroups.com
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:

Ajit Teli

unread,
May 23, 2013, 5:55:02 AM5/23/13
to rubyonra...@googlegroups.com
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,

Dheeraj Kumar

unread,
May 23, 2013, 6:16:36 AM5/23/13
to rubyonra...@googlegroups.com
use `next if (i % 10 != 0)`

-- 
Dheeraj Kumar

--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

jsnark

unread,
May 23, 2013, 8:52:13 AM5/23/13
to rubyonra...@googlegroups.com
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.

Robert Walker

unread,
May 23, 2013, 9:50:53 AM5/23/13
to rubyonra...@googlegroups.com
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

Rob Biedenharn

unread,
May 23, 2013, 12:54:13 PM5/23/13
to rubyonra...@googlegroups.com
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]]


Colin Law

unread,
May 23, 2013, 3:08:47 PM5/23/13
to rubyonra...@googlegroups.com
On 23 May 2013 13:52, jsnark <s...@monmouth.com> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> 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.
Reply all
Reply to author
Forward
0 new messages