break from select

V

Venkat Surabhi

Hi,

I have an array like a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
I need to select elements which are divisible by 2 and select only
5 elements.

col = a.select do |e|
e %2 == 0
end

I get all the elements into col which are divisible by 2 and working
fine.

what if I want to select only 5 elements
I am trying below code
i = 0
col = a.select do |e|
i += 1
break if i > 5
e %2 == 0
end

Now col returns nil.
Also used return but getting error.

Any solution for this.

Thanks,
Venkat
 
M

Mayank Kohaley

[Note: parts of this message were removed to make it a legal post.]

a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
a = a.select { |x| x%2 == 0 }
a = a.slice(0,5)
puts "#{a.inspect}"

Is this what you are expecting?


Hi,

I have an array like a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
I need to select elements which are divisible by 2 and select only
5 elements.

col = a.select do |e|
e %2 == 0
end

I get all the elements into col which are divisible by 2 and working
fine.

what if I want to select only 5 elements
I am trying below code
i = 0
col = a.select do |e|
i += 1
break if i > 5
e %2 == 0
end

Now col returns nil.
Also used return but getting error.

Any solution for this.

Thanks,
Venkat
 
S

Saravanan Krishnan

Hi Venkat,

Quick Solution,
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
b =[]
a.collect{|x| b << x if x %2 == 0 and b.length<5}
puts b.inspect

Cheers,
Sarav

Venkat Surabhi wrote in post #989134:
Hi,

I have an array like a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
I need to select elements which are divisible by 2 and select only
5 elements.

col = a.select do |e|
e %2 == 0
end

I get all the elements into col which are divisible by 2 and working
fine.

what if I want to select only 5 elements
I am trying below code
i = 0
col = a.select do |e|
i += 1
break if i > 5
e %2 == 0
end

Now col returns nil.
Also used return but getting error.

Any solution for this.

Thanks,
Venkat
 
V

Venkat Surabhi

Thanks Saravanan for your reply. I thought of doing this .
But the problem is array size. Here I defined array with 15 elements.
What if we have 1000 of elements in array.

Below code a.collect will iterate for all the elements even if I
achieved 5 elements.



Saravanan Krishnan wrote in post #989140:
Hi Venkat,

Quick Solution,
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
b =[]
a.collect{|x| b << x if x %2 == 0 and b.length<5}
puts b.inspect

Cheers,
Sarav
 
S

Saravanan Krishnan

Hi venkat,

Here is the better solution,

a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
b = []
a.each do |x|
next if b.length >= 5
b << x if x % 2 ==0
end

puts b

Cheers,
Sarav

Venkat Surabhi wrote in post #989142:
 
R

Robert Klemme

Hi venkat,

Here is the better solution,

a =3D [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
b =3D []
a.each do |x|
=A0next if b.length >=3D 5
=A0b << x if x % 2 =3D=3D0
end

puts b

You are still iterating the whole input Array. This is better

irb(main):001:0> a =3D 1.upto(15).to_a
=3D> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
irb(main):002:0> b =3D []
=3D> []
irb(main):003:0> a.each {|x| if x % 2 =3D=3D 0; b << x; break if b.size >=
=3D 5 end}
=3D> nil
irb(main):004:0> b
=3D> [2, 4, 6, 8, 10]

Insert "p x" at beginning of block to see that we really short circuit.

If you want to get fancy you can do this as well:

irb(main):010:0> b =3D []
=3D> []
irb(main):011:0> a.each {|x| p x; x % 2 =3D=3D 0 and b << x and b.size >=3D
5 and break}
1
2
3
4
5
6
7
8
9
10
=3D> nil

We can even integrate this with #inject to get a *cough* one liner:

irb(main):012:0> b =3D a.inject([]) {|c,x| p x; x % 2 =3D=3D 0 and c << x
and c.size >=3D 5 and break c or c}
1
2
3
4
5
6
7
8
9
10
=3D> [2, 4, 6, 8, 10]

:)

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
S

Saravanan Krishnan

Yes Robert you are right... I've used "next" instead of "break"...


Robert K. wrote in post #989144:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,242
Latest member
KendrickKo

Latest Threads

Top