Forum: Ruby Comparing two arrays

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rafael G. (Guest)
on 2007-02-27 19:42
(Received via mailing list)
Hi, im trying to compare two arrays and creating a new one with the
values that are in the second array and the values that aren't
concatenated with "NOT FOUND", here is the snippet code. Im getting a
weird result im getting the same values saying "not found" and without
the concatanation, can somebody please give me a hint of what is going
on.

Thanks in advance.

 def handle_data(data1,data2)
   c = 0
   t = 0
   result = []
  while c < data1.length
    k = 0
    while k < data2.length
      if data1[c].length > 7
         result[t] = data1[c].include?(data2[k]) ? data1[c] :
data1[c]+" NOT FOUND"
         t += 1
      end
      k += 1
    end
    c += 1
   end
   return result.uniq
 end
Robert D. (Guest)
on 2007-02-27 19:58
(Received via mailing list)
On 2/27/07, Rafael G. <removed_email_address@domain.invalid> wrote:
>    c = 0
>       k += 1
>
>
If I understand correctly you want something like

ary1.map{ |ele| ele.to_s << ( ary2.include?( ele ) ? "" : " NOT FOUND" )
}

HTH
Robert

BTW did I read an article of you in the French Linux Mag recently?
Rafael G. (Guest)
on 2007-02-27 20:05
(Received via mailing list)
Ok, from what i understand you are iterating thought the entire first
array and then testing the content of it in the second, thats seems
ok. Let me check if that can solve my problem.

And, related to the article i don't know for sure, i wrote something
in the past but it was in spanish can you point me to the article or
at least the title of it.

Thanks.
Gavin K. (Guest)
on 2007-02-27 20:16
(Received via mailing list)
On Feb 27, 10:41 am, "Rafael G." <removed_email_address@domain.invalid> wrote:
> Hi, im trying to compare two arrays and creating a new one with the
> values that are in the second array and the values that aren't
> concatenated with "NOT FOUND", here is the snippet code.

Ruby makes it quite easy to find out which elements from one array
aren't in the other:

irb(main):001:0> a = [  'a', 'b', 'c', 'd' ]
=> ["a", "b", "c", "d"]
irb(main):002:0> b = [ 'c', 'b', 'd', 'e' ]
=> ["c", "b", "d", "e"]
irb(main):003:0> a - b    # Items from a not in b.
=> ["a"]
irb(main):004:0> b - a    # Items from b not in a.
=> ["e"]
irb(main):006:0> a | b    # Items shared between both.
=> ["a", "b", "c", "d", "e"]
Rafael G. (Guest)
on 2007-02-27 20:43
(Received via mailing list)
Ok, thanks now im trying to take out this from a anyline on the file

-----
or
[a-zA-Z] or #

this is the code that im using, but the lines stays with something
because my script parse that line. Another suggestion ?
.delete("#[a-zA-Z]-").strip
Gavin K. (Guest)
on 2007-02-27 20:56
(Received via mailing list)
On Feb 27, 11:42 am, "Rafael G." <removed_email_address@domain.invalid> wrote:
> Ok, thanks now im trying to take out this from a anyline on the file
>
> -----
> or
> [a-zA-Z] or #
>
> this is the code that im using, but the lines stays with something
> because my script parse that line. Another suggestion ?
> delete("#[a-zA-Z]-").strip

I'm sorry, but I do not understand at all what you are asking. I
assume English isn't your first language (and that's OK). Could you
try writing it a different way, so maybe I can help you?

P.S. It's considered bad form on the Internet and particularly this
forum to "top post", putting your reply above the original text. In
the future, please try to put your reply after  the quoted text, so
that the original flow of responses continues from top to bottom.
Rafael G. (Guest)
on 2007-02-27 21:08
(Received via mailing list)
On 2/27/07, Phrogz <removed_email_address@domain.invalid> wrote:
>
>
Sorry for that about the post i did not know. Maybe if i show you some
code you can check what am i trying to do.

Anyway i will try to make a simple explanation:
I parsing a CSV file using the module from the Ruby standard lib, by
far i have the parsing ok and the comparation too. So the problem is
that i don't need to fieldnames to get in the comparation process, so
i was trying to take out the letters and - and # from the output. The
problem now is that i just notice that some columns in the data have
letters too so if i take out that it won't make any sense.

If you have a suggestion can be a of a lot of help, thanks


  data = []
 i = 0
  IO.foreach(filename) do |line|
     cols = []
    cols=CSV::parse_line line.delete("#[a-zA-Z]-").strip
     if (cols[args[0]] != nil) && (cols[args[1]] != nil)
       if cols[args[0]].slice(0,1).to_i > 0 && cols[args[0]].length < 7-
         cols[args[0]].chop.strip
        cols[args[1]].chop.strip
          data[i] = "0"+cols[args[0]]+"-"+cols[args[1]]
       else
          data[i] = cols[args[0]].sub(/[^\w]/,'')+"-"+cols[args[1]]
       end
       i += 1
     end
 79   end
 80   return data
Robert D. (Guest)
on 2007-02-27 22:26
(Received via mailing list)
On 2/27/07, Rafael G. <removed_email_address@domain.invalid> wrote:
> > > delete("#[a-zA-Z]-").strip
> >
> problem now is that i just notice that some columns in the data have
>      if (cols[args[0]] != nil) && (cols[args[1]] != nil)
>  80   return data
>
>
> --
> Grimoire Guru
> SourceMage GNU/Linux
>
>
Rafael it is indeed confusing, I guess I did not understand what you
wanted, why not post some data samples, or maybe it's just me.
For the article no, was my error I have misread your signature it was
"Grégiore" not "Grimoire", well maybe I am just muy cansado, si poedes
tambien ecrirme OFFLIST en castellano si quieres.

cheers
Robert
This topic is locked and can not be replied to.