Find matching elements in array

P

Pragash Mr.

Hi,
I have one array for example[1,2,3,4]

i need to find the matching elements of another array is there any way
to find....
for example [1,2,3,4].include?(2)
it will return true
but i need to find one or more elements
example : [1,2,3,4].include?(2,3)
it is throwing an error



If you have solution reply me

Thanx
Pragash
 
M

Mikel Lindsaar

class Array
def contains?(array)
(self & array) == array
end
end

Note however, this will only work if the match is in order.

For example

[1,2,3,4].contains?([2,1]) #=> false

But

[1,2,3,4].contains?([1,4]) #=> true

That might be what you want. If not, you could call sort on it as well.

ANyway... the point is, you can have a look at the ruby rdoc and find
a lot about this. Arrays have been around for a while and Ruby
handles them more elegantly than any other language I have used.

Go have a read: http://www.ruby-doc.org/core/classes/Array.html

Mikel
 
S

Sandor Szücs

I have one array for example[1,2,3,4]

i need to find the matching elements of another array is there any way
to find....
for example [1,2,3,4].include?(2)
it will return true
but i need to find one or more elements
example : [1,2,3,4].include?(2,3)
it is throwing an error



If you have solution reply me


class Array
alias old_include? include?
def include?(*args)

if args.length <=3D 1
old_include?(args)
else
args.all? do |a|
old_include?(a)
end
end
end
end

puts("#{(0..9).to_a.include?(3,5)}") # true
puts("#{('a'..'z').to_a.include?(3,5)}") # false
puts("#{('A'..'ZZ').to_a.include?('G','AB')}")# true

regards, Sandor Sz=FCcs
--
 
J

Jeff

Hi,
I have one array for example[1,2,3,4]

i need to find the matching elements of another array is there any way
to find....
for example [1,2,3,4].include?(2)
it will return true
but i need to find one or more elements
example : [1,2,3,4].include?(2,3)
it is throwing an error

If you have solution reply me

Thanx
Pragash

How about something like this?

a =3D [1,2,3,4]
x =3D [2,3]

x.all? { |n| a.include?(n) }

(warning, this is slow for large arrays)

Jeff
 
Y

Yossef Mendelssohn

class Array
alias old_include? include?
def include?(*args)

if args.length <=3D 1
old_include?(args)
else
args.all? do |a|
old_include?(a)
end
end
end
end

puts("#{(0..9).to_a.include?(3,5)}") # true
puts("#{('a'..'z').to_a.include?(3,5)}") # false
puts("#{('A'..'ZZ').to_a.include?('G','AB')}")# true

regards, Sandor Sz=FCcs
--

Two things:

1) This doesn't work. Try ('a'..'z').to_a.include?('a')
2) The length check isn't necessary. It's sufficient to just use
args.all? { |a| old_include?(a) }, assuming it's okay that calling
include? with no arguments now returns true.

I like Mikel's set-intersection solution (which, incidentally, is what
I was thinking), assuming once again that the order is important.
 
S

Sandor Szücs

Hi,

Two things:

1) This doesn't work. Try ('a'..'z').to_a.include?('a')
ack.

2) The length check isn't necessary. It's sufficient to just use
args.all? { |a| old_include?(a) }, assuming it's okay that calling
include? with no arguments now returns true.
ack.

I like Mikel's set-intersection solution (which, incidentally, is what
I was thinking), assuming once again that the order is important.

That's also what I thought if I read that e-mail.

regards, Sandor Sz=FCcs
--=
 
Y

Yossef Mendelssohn

2) The length check isn't necessary. It's sufficient to just use
args.all? { |a| old_include?(a) }, assuming it's okay that calling
include? with no arguments now returns true.

I'd be in favor of keeping that safeguard, partly because it feels
weird for [1,2,3].include?() to be true and partly because there might
be some edge case or test where having it succeed silently might be
problematic.

Oh, I'd probably put a safeguard in there, possibly a check on the
number of arguments so that [1,2,3].include?() still raises an
ArgumentError.

My comment about the length check not being necessary wasn't a
judgement of how I would write it
as much as it was just saying that code doesn't perform any useful
function in that context. Personally, I think [].all? shouldn't be
true, but that debate has happened before.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top