Find matching elements in array

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

···

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

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

Depends what you want, if you just want to find out if the numbers 2
and 3 are in the other array, you could use:

== Array RDoc:
array & other_array
Set Intersection—Returns a new array containing elements common to
the two arrays, with no duplicates.
[ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ]

so you could do something like this:

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

=> nil

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

[1,2,3,4].contains?([1,9])

=> false

[1,2,3,4].contains?([12,3])

=> false

[1,2,3,4].contains?([2,3])

=> true

Mikel

···

On Mon, Jul 7, 2008 at 4:04 PM, Pragash Mr. <gananapragasam@srishtisoft.com> wrote:

--

Rails, RSpec and Life blog....

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

     if args.length <= 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ücs

···

On 07.07.2008, at 08:04, Pragash Mr. wrote:

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

--

How about something like this?

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

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

(warning, this is slow for large arrays)

Jeff

···

On Jul 7, 1:04 am, "Pragash Mr." <gananapraga...@srishtisoft.com> wrote:

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
--
Posted viahttp://www.ruby-forum.com/.

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: class Array - RDoc Documentation

Mikel

···

--

Rails, RSpec and Life blog....

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.

···

On Jul 7, 1:32 am, Sandor Szücs <sandor.szu...@fu-berlin.de> wrote:

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

     if args.length <= 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ücs
--

--
-yossef

Hi --

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

     if args.length <= 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ücs
--

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'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.

A case statement might be a nice way to handle it:

   def include?(*args)
     case args.size
     when 0
       old_include?
     else
       args.all? {|a| old_include?(a) }
     end
   end

I also think this one is kind of cool, though I would assume it's a
good bit slower:

   def include?(head,*tail)
     case tail
     when
       old_include?(head)
     else
       include?(head) && include?(*tail)
     end
   end

David

···

On Tue, 8 Jul 2008, Yossef Mendelssohn wrote:

On Jul 7, 1:32 am, Sandor Szücs <sandor.szu...@fu-berlin.de> wrote:

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails July 21-24 Edison, NJ
   Advancing With Rails August 18-21 Edison, NJ
See http://www.rubypal.com for details and updates!

Hi,

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

     if args.length <= 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ücs
--

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ücs

···

On 08.07.2008, at 02:24, Yossef Mendelssohn wrote:

On Jul 7, 1:32 am, Sandor Szücs <sandor.szu...@fu-berlin.de> wrote:

--

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.

···

On Jul 7, 9:12 pm, "David A. Black" <dbl...@rubypal.com> wrote:

On Tue, 8 Jul 2008, Yossef Mendelssohn wrote:
> 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.

--
-yossef