Comparing two arrays

Discussion in 'Ruby' started by Rafael George, Feb 27, 2007.

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

    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
    k += 1
    c += 1
    return result.uniq
    Rafael George, Feb 27, 2007
    1. Advertisements

  2. Rafael George

    Robert Dober Guest

    If I understand correctly you want something like{ |ele| ele.to_s << ( ary2.include?( ele ) ? "" : " NOT FOUND" ) }


    BTW did I read an article of you in the French Linux Mag recently?
    Robert Dober, Feb 27, 2007
    1. Advertisements

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

    Rafael George, Feb 27, 2007
  4. Rafael George

    Phrogz Guest

    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"]
    Phrogz, Feb 27, 2007
  5. Ok, thanks now im trying to take out this from a anyline on the file

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

    Rafael George, Feb 27, 2007
  6. Rafael George

    Phrogz Guest

    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.
    Phrogz, Feb 27, 2007
  7. 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-
    data = "0"+cols[args[0]]+"-"+cols[args[1]]
    data = cols[args[0]].sub(/[^\w]/,'')+"-"+cols[args[1]]
    i += 1
    79 end
    80 return data
    Rafael George, Feb 27, 2007
  8. Rafael George

    Robert Dober Guest

    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=E9giore" not "Grimoire", well maybe I am just muy cansado, si poedes
    tambien ecrirme OFFLIST en castellano si quieres.


    We have not succeeded in answering all of our questions.
    In fact, in some ways, we are more confused than ever.
    But we feel we are confused on a higher level and about more important thin=
    Robert Dober, Feb 27, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.