title: Ruby: How to append unique values to duplicates? E.g (value, value_2, value_3)
body: <p>I'm using software that won't allow duplicate values for the field "Node_ID", the nodes are a collection of objects with multiple fields (e.g X co-ordinate, Y co-ordinate, elevation). I am looking to rename the Node_Ids for all the nodes to a new format, but struggling to append a unique number for any duplicates encountered.</p>
  
  <p>Based on other threads I've read, I have tried to located any duplicates using the following:</p>
  
  <pre><code>array.include?(node)
  
  duplicates = array.select{|element| array.count(element) &gt; 1}
  </code></pre>
  
  <p>I'm at a point where the code appears to recognise a duplicate and appends "_1" to the duplicate node, however if there is yet another duplicate it also appends "_1"...</p>
  
  <pre><code>$array = Array.new    # this is a temporary array I have been using to store the written Node_IDs, to check against for duplicates.
  xy_nodes = ('_nodes').each do |xy|
      x1 = xy.x.to_i
      y1 = xy.y.to_i
      x = x1.to_s.rjust(7, "0")
      y = y1.to_s.rjust(7, "0")
      node = x+y
          # the above was just getting it to the correct format, no issues there
      if $array.include?(node)
          i=0
          node = node + "_#{i+1}"    # this is where things need sorted, clearly as it stands this would only result in _1 being added for the duplicates, however I'm struggling to iterate and check against duplicates after the first to continue adding _1, _2, _3 as suffixes.
          $array &lt;&lt; node
          $checkmsg &lt;&lt; node    # this array is used later
          xy.user_text = node    # used for writing to as this field can accept duplicates
          xy.write
      else
          puts "...iteration found no duplicates"
          $array &lt;&lt; node
          xy.user_text = node
          xy.write        
      end
      puts "************ END OF ITERATION **************"
      puts ""
  end
  </code></pre>
  
  <p>This results in something similar to below (in "user_text" field):</p>
  
  <pre><code>05555550333333
  04444440222222
  05555550333333_1
  05555550333333_1
  </code></pre>
  
  <p>Just wondering how best to get the code to assess the new duplicate and count upward for each one found and get:</p>
  
  <pre><code>05555550333333
  04444440222222
  05555550333333_1
  05555550333333_2
  </code></pre>
  
  <p>Thanks.</p>
  
  <p><strong>UPDATE: In response to answers</strong></p>
  
  <p>I was having trouble squeezing in info to the comments so I thought I'd write here. First off, thanks for answering so far. All three techniques appear to work well in making $array look what I'm hoping for the finished article of the Node_IDs (or user_text whilst testing) to be.</p>
  
  <p>For example, using these methods I'm able to get the $array output:</p>
  
  <pre><code>02511160678961
  02735510688965
  02966900697649
  03216480682699
  02735510688965_1
  02735510688965_2
  02735510688965_3
  03355411149097
  </code></pre>
  
  <p>However, the $array in my code was originally just used as something I was throwing "node" into, in order to check against to see if it had been used before, rather than being an array I wanted to format/write itself.</p>
  
  <p>Each node is an object within the table ('_nodes'). And I'm hoping to iteratively write the new user_text (Node_ID) checking for duplicates each iteration, rather than at the end of a completed array.</p>
  
  <p>My aim was to update each object's user_text (eventually Node_ID once solved) and add it to the $array for checking for duplicates when the next object's user_text is going to be written. Not sure if there is an obvious way to apply the suggestions so far to each object before writing - rather than to a completed array like I have managed with your help.</p>
  
  <p>Also not sure if I am being entirely clear - please ask if not and I can edit my initial post further. Also happy to abandon my initial thought process if it seems like its the wrong train of thought.  Thanks again.</p>