[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

find if an array has any element present in another array

Charanya Nagarajan

4/30/2009 10:52:00 AM

Hi,

I have 2 lists

a=[1,2,3,4]
b=[2,4,6,7,8]

I want to find if there are any elements present in "a" which are also
present in "b".
I jus wanna "true" or "false" answer if both of the arrays have any
element in common.

Is there any inbuilt functions like "include?" for this purpose??

Thanks in Advance.
--
Posted via http://www.ruby-....

8 Answers

as

4/30/2009 10:57:00 AM

0

Le Thu, 30 Apr 2009 05:52:26 -0500,
Charanya Nagarajan <charanya.nagarajan@tcs.com> a écrit :

> I have 2 lists
>
> a=[1,2,3,4]
> b=[2,4,6,7,8]
>
> I want to find if there are any elements present in "a" which are also
> present in "b".
> I jus wanna "true" or "false" answer if both of the arrays have any
> element in common.

(a-b).nil?

--
Arnaud Schmittbuhl

7stud --

4/30/2009 11:00:00 AM

0

Charanya Nagarajan wrote:
> Hi,
>
> I have 2 lists
>
> a=[1,2,3,4]
> b=[2,4,6,7,8]
>
> I want to find if there are any elements present in "a" which are also
> present in "b".
> I jus wanna "true" or "false" answer if both of the arrays have any
> element in common.
>
> Is there any inbuilt functions like "include?" for this purpose??
>
> Thanks in Advance.

a=[1,2,3,4]
b=[2,4,6,7,8]

result = a & b
p result

puts (a & b).empty?

--output:--
[2, 4]
false

--
Posted via http://www.ruby-....

Farrel Lifson

4/30/2009 11:04:00 AM

0

2009/4/30 Charanya Nagarajan <charanya.nagarajan@tcs.com>:
> I have 2 lists
>
> a=[1,2,3,4]
> b=[2,4,6,7,8]
>
> I want to find if there are any elements present in "a" which are also
> present in "b".
> I jus wanna "true" or "false" answer if both of the arrays have any
> element in common.

You can take the intersection of the two and see if it's empty.

if (a & b).empty?
puts( 'Nothing in common' )
else
puts( 'Common elements')
end

Farrel
--
Aimred - Ruby Development and Consulting
http://www....

Charanya Nagarajan

4/30/2009 11:12:00 AM

0

Thank you.This was What i wanted
--
Posted via http://www.ruby-....

as

4/30/2009 11:12:00 AM

0

Le Thu, 30 Apr 2009 12:57:23 +0200,
as <nosp@m.invalid> a écrit :

> Le Thu, 30 Apr 2009 05:52:26 -0500,
> Charanya Nagarajan <charanya.nagarajan@tcs.com> a écrit :
>
> > I have 2 lists
> >
> > a=[1,2,3,4]
> > b=[2,4,6,7,8]
> >
> > I want to find if there are any elements present in "a" which are
> > also present in "b".
> > I jus wanna "true" or "false" answer if both of the arrays have any
> > element in common.
>
> (a-b).nil?

Ouch, sorry, (a&b).empty?


--
Arnaud Schmittbuhl

7stud --

4/30/2009 11:15:00 AM

0

as wrote:
>
> (a-b).nil?

result = [1] - [2, 3] #no common elements
p result

result = [2, 3] - [2] #common elements
p result

--output:--
[1]
[3]

--
Posted via http://www.ruby-....

Bert Hyman

7/23/2010 4:40:00 PM

0

In news:7b9j46pnn83va0nkpight9nl735h3oarqq@4ax.com Johannes von
Ebersdorf <ebersdorf@gmail.com> wrote:

> Germany has very little for which to be thankful to the USA,

Still upset about the way the war turned out, I see.

Hoping for a 4th Reich?

--
Bert Hyman St. Paul, MN bert@iphouse.com

Johannes von Ebersdorf

7/23/2010 9:53:00 PM

0

On 23 Jul 2010 16:40:19 GMT, Bert Hyman <bert@iphouse.com> wrote:

>In news:7b9j46pnn83va0nkpight9nl735h3oarqq@4ax.com Johannes von
>Ebersdorf <ebersdorf@gmail.com> wrote:
>
>> Germany has very little for which to be thankful to the USA,
>
>Still upset about the way the war turned out, I see.

I just get fed up with American hypocrisy and obvious lies. They are
not a class act.

>
>Hoping for a 4th Reich?

According to the German government, there has been no new German state
since 1871, so we seem to be still working on the Second Empire, not
the third or fourth.