Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Find matching elements in array

Reply
Thread Tools

Find matching elements in array

 
 
Pragash Mr.
Guest
Posts: n/a
 
      07-07-2008
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/.

 
Reply With Quote
 
 
 
 
Mikel Lindsaar
Guest
Posts: n/a
 
      07-07-2008
> 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


--
http://lindsaar.net/
Rails, RSpec and Life blog....

 
Reply With Quote
 
 
 
 
Sandor Szücs
Guest
Posts: n/a
 
      07-07-2008

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



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




 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      07-07-2008
On Jul 7, 1:04=A0am, "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/.


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

 
Reply With Quote
 
Yossef Mendelssohn
Guest
Posts: n/a
 
      07-08-2008


On Jul 7, 1:32 am, Sandor Sz=FCcs <sandor.szu...@fu-berlin.de> wrote:
> 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.

--
-yossef

 
Reply With Quote
 
Sandor Szücs
Guest
Posts: n/a
 
      07-08-2008
Hi,

On 08.07.2008, at 02:24, Yossef Mendelssohn wrote:
> On Jul 7, 1:32 am, Sandor Sz=FCcs <sandor.szu...@fu-berlin.de> wrote:
>> 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')


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
--=

 
Reply With Quote
 
Yossef Mendelssohn
Guest
Posts: n/a
 
      07-11-2008
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.


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.

--
-yossef

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
find a matching pattern in file and find it in another file too nani Perl Misc 2 03-14-2008 05:20 AM
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM
Find if elements of one array are present in the other and return a boolean array Shalini C++ 2 01-09-2004 06:13 PM
looping through array to find next matching element Psybar Phreak Java 1 10-06-2003 06:01 PM



Advertisments
 



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 51 52 53 54 55 56 57