Find matching elements in array

Discussion in 'Ruby' started by Pragash Mr., Jul 7, 2008.

  1. Pragash Mr.

    Pragash Mr. Guest

    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/.
     
    Pragash Mr., Jul 7, 2008
    #1
    1. Advertising

  2. > 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....
     
    Mikel Lindsaar, Jul 7, 2008
    #2
    1. Advertising

  3. 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
    --
     
    Sandor Szücs, Jul 7, 2008
    #3
  4. Pragash Mr.

    Jeff Guest

    On Jul 7, 1:04=A0am, "Pragash Mr." <>
    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
     
    Jeff, Jul 7, 2008
    #4
  5. On Jul 7, 1:32 am, Sandor Sz=FCcs <> 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
     
    Yossef Mendelssohn, Jul 8, 2008
    #5
  6. Hi,

    On 08.07.2008, at 02:24, Yossef Mendelssohn wrote:
    > On Jul 7, 1:32 am, Sandor Sz=FCcs <> 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
    --=
     
    Sandor Szücs, Jul 8, 2008
    #6
  7. On Jul 7, 9:12 pm, "David A. Black" <> 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
     
    Yossef Mendelssohn, Jul 11, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Psybar Phreak
    Replies:
    1
    Views:
    573
    Eric Sosman
    Oct 6, 2003
  2. Shalini
    Replies:
    2
    Views:
    505
    Brian Genisio
    Jan 9, 2004
  3. P
    Replies:
    1
    Views:
    1,181
    Joe Kesselman
    Jul 7, 2006
  4. Stefan Behnel
    Replies:
    0
    Views:
    623
    Stefan Behnel
    Aug 31, 2010
  5. nani
    Replies:
    2
    Views:
    171
    comp.llang.perl.moderated
    Mar 14, 2008
Loading...

Share This Page