I want to count the occurrence of specific words. In the past I’ve
used a hash as shown below. Is there a better way? I don’t know which
words will occur, otherwise I’d just preinitialize the hash to all
zeros.
Thanks, Andrew
foo = Hash.new
bar = Hash.new
while l = gets
l.split.each { |w|
# option 1: try to increment cell, catch error
begin
foo[w] += 1
rescue NameError
foo[w] = 1
end
# option 2: initialize on first use
bar[w] = 0 if !bar[w]
bar[w] += 1
}
end
puts foo
puts bar
I want to count the occurrence of specific words. In the past I've
used a hash as shown below. Is there a better way? I don't know which
words will occur, otherwise I'd just preinitialize the hash to all
zeros.
try:
wordcnt = Hash.new(0)
while l = gets.split.each do |w|
wordcnt[w] += 1
end
puts wordcnt
regards,
-joe
andrew queisser wrote:
I want to count the occurrence of specific words. In the past I’ve
used a hash as shown below. Is there a better way? I don’t know which
words will occur, otherwise I’d just preinitialize the hash to all
zeros.
Thanks, Andrew
foo = Hash.new
bar = Hash.new
while l = gets
l.split.each { |w|
# option 1: try to increment cell, catch error
begin
foo[w] += 1
rescue NameError
foo[w] = 1
end
# option 2: initialize on first use
bar[w] = 0 if !bar[w]
bar[w] += 1
Two other approaches to opt 2:
bar[w] ||= 0
bar[w] += 1
and
bar[w] = bar[w].to_i + 1
···
}
end
puts foo
puts bar
andrew_queisser@Hp.com (andrew queisser) writes:
I want to count the occurrence of specific words. In the past I’ve
used a hash as shown below. Is there a better way? I don’t know which
words will occur, otherwise I’d just preinitialize the hash to all
zeros.
# option 1: try to increment cell, catch error
# option 2: initialize on first use
option 3: use Hash’s default value
counts = Hash.new(0)
ARGF.each {|line| line.scan(/\w+/).each {|w| counts[w] += 1}}
counts.keys.sort.each {|k| printf(“%4d: %s\n”, counts[k], k) }
Cheers
Dave