Converting a raw list of IDs to a consolidated array of ids [closed]

Posted on


I feel I have overcomplicated the process. What can I do to improve the following code?

ids = [], "r").each_line do |line|
  i += 1

  parts = line.split(',')

  parts[1] = parts[1].to_i
  parts[2] = parts[2].to_i

  # If the id doesn't exist in our ids array then create it and add the
  # second id else delete the second id and merge it into the new id
  unless defined? ids[parts[2]]
    # Instantiate the array first
    ids[parts[1]] = [] unless defined? ids[parts[1]]

    # Add the new id to the array
    # Move over older ids that matched these to the new one
    if ids[parts[1]].nil?
      unless ids[parts[2]].nil?
        ids[parts[1]] = ids[parts[2]]
        ids[parts[1]] = [parts[2]]
      unless ids[parts[2]].nil?
        ids[parts[2]].each do |id|

    # Insert the old id into the new array

    # Delete the old one
    ids.delete(parts[2]) unless ids[parts[2]].nil?

The file I am processing is being generated by a deduping library, Duke.

I am trying to consolidate the matches it has generated into a grouped list of matched ids, so all common ids are grouped together. The file itself is 2.4G.

The point of consolidating this is that these are many purchases that have been ran through a deduplication process to product a single customer. I feel it would be easier to consolidate this list rather than building up the customer and continually add purchases to it. This is a one time process.

Here’s the first 100 lines from the file I’m consolidating. The format of which is:




Your code is very, very broken. Technically, that means it’s not a question for CodeReview. However, I’ll review it anyway, only because it’s, well, it’s fascinatingly broken.

Name your variables. Right now, it’s really difficult to keep track of things, because parts[1] and parts[2] is pretty obtuse. They’re obviously integer IDs, but what’s their relation to each other?

Reading it felt something like this:

There’s an ID and an ID and an array, and if the ID – yeah, that one – is in the array, then add the ID – no, that one – but if the ID isn’t… wait

A comment like this doesn’t really help:

# If the id doesn't exist in our ids array then create it and add the
# second id else delete the second id and merge it into the new id

I’m sorry, but what? What is “the id”, and “the second id”, and “the new id”? All I have is parts[1] and parts[2].

Secondly, don’t use unless...else. Ruby’s unless is great for single conditional statements like doStuff unless x, but if you’re going to have an else branch, just use a regular old if..else. Otherwise the else branch reads like a double-negative: “If not, do this; else if not not, do this”. It gets confusing in a hurry.

Now, as to why it’s actually broken, and not just hard to read:

defined? doesn’t do what you think it does. If you’ve got an array a = [1,2,3], and you try defined?(a[21315]) what do you get? The string "method". Which is truthy. So the entire first branch never runs. Checking for nil? is what you want to be doing, and you do that in other places – which, even if both approaches worked, also makes the code inconsistent.

And here’s why it’s really broken: Array#delete. It, as the name suggests, deletes things. It removes the given index entirely, shifting all following indices one place. And you’re relying on ids being equal to array indices. See the problem? You may have assigned something to ids[23], but then you went and called ids.delete(10), and what you previously assigned as ids[23] is now ids[22]. And the next time you go look up ID 23, you’ll get get what used to be ids[24].

So even if the code somehow worked, and chewed through your file, you’d get very, very wrong results.

Oh, and by the way, what is i doing? Why are you incrementing it?

Overall, I can’t tell you what to do instead, because frankly I’m not even sure what it’s supposed to do right now.

Leave a Reply

Your email address will not be published. Required fields are marked *