Parent

Included Modules

Class/Module Index [+]

Quicksearch

Array

Public Instance Methods

recode_repeated() click to toggle source

Recode repeated values on an array, adding the number of repetition at the end Example:

a=%w{a b c c d d d e}
a.recode_repeated
=> ["a","b","c_1","c_2","d_1","d_2","d_3","e"]
# File lib/statsample.rb, line 63
def recode_repeated
  if self.size!=self.uniq.size
    # Find repeated
    repeated=self.inject({}) {|a,v|
    (a[v].nil? ? a[v]=1 : a[v]+=1); a }.find_all{|k,v| v>1}.collect{|k,v| k}
    ns=repeated.inject({}) {|a,v| a[v]=0;a}
    self.collect do |f|
      if repeated.include? f
        ns[f]+=1
        sprintf("%s_%d",f,ns[f])
      else
        f
      end
    end
  else
    self
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.