Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/crosstab/extensions.rb

Instance Method Summary collapse

Instance Method Details

#to_freq_chartObject

Converts a flat list to a frequency chart.

Example:

[nil, "a", "a", "a", "b", "b", "b", nil, "c"].to_freq_chart
# => [[1,nil],[3,"a"],[3,"b"],[1,nil],[1,"c"]]


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/crosstab/extensions.rb', line 10

def to_freq_chart
  # pre_state: [nil, a, a, a, b, b, b, nil, c]        
  bland_array = self.collect { |x| [1,x] }
      
  # pre_state: [[1,nil],[1,a],[1,a],[1,a],[1,b],[1,b],[1,b],[1,nil],[1,c]]
  final_array = []
  bland_array.each do |x|
    if final_array.length > 0 and final_array.last[1] == x[1]
      final_array.last[0] += 1
    else
      final_array << x
    end
  end
 
  final_array
end