Manipulating Strings and Arrays in Ruby
I've got:
@fruit = ["Apples, Oranges, Bananas", "Apples", "Bananas, Pears", "Bananas, Apples, Pears", "Pears"]
I want to do two different things with this, first turn it into a pure array with only on开发者_StackOverflowe instance of each:
["Apples", "Oranges", "Bananas", "Pears"]
Secondly I want to be able to determine how many of a given instance there are in the array:
@fruit.count("Apples") = 3
Thirdly, is it possible to order the array by the number of instances:
@fruit.sort = ["Apples", "Apples", "Apples", "Bananas", "Bananas", "Bananas", "Pears", "Pears", "Pears", "Oranges"]
What array/string functions would I have to use to do this?
arr = ["Apples, Oranges, Bananas", "Apples", "Bananas, Pears", "Bananas, Apples, Pears", "Pears"]
hsh = Hash.new { |h, k| h[k] = 0 }
arr.each do |str|
fruits = str.split(/, /)
fruits.each do |fruit|
hsh[fruit] += 1
end
end
p hsh.keys
# => ["Apples", "Oranges", "Bananas", "Pears"]
hsh.keys.each { |fruit| puts "#{fruit}: #{hsh[fruit]}" }
# => Apples: 3
# => Oranges: 1
# => Bananas: 3
# => Pears: 3
@fruit = ["Apples, Oranges, Bananas", "Apples", "Bananas, Pears", "Bananas, Apples, Pears", "Pears"]
@fruits = @fruit.map{|f| f.split(", ")}.flatten
#=>["Apples", "Oranges", "Bananas", "Apples", "Bananas", "Pears", "Bananas", "Apples", "Pears", "Pears"]
@fruits.uniq
#=> ["Apples", "Oranges", "Bananas", "Pears"]
@fruits.count{|f| f=="Apples"}
#=>3
A hash is the better data structure to do this:
@fruit = ["Apples, Oranges, Bananas", "Apples", "Bananas, Pears", "Bananas, Apples, Pears", "Pears"]
h = Hash.new
@fruit.each do |str|
str.split(/,/).each do |f|
f.strip!
h[f] ||= 0
h[f] += 1
end
end
h.keys
=> ["Apples", "Oranges", "Bananas", "Pears"]
h
=> {"Apples"=>3, "Oranges"=>1, "Bananas"=>3, "Pears"=>3}
h["Apples"]
=> 3
you can then process the accumulated data in the hash to print out a sorted array if you need one.
@fruit = ["Apples, Oranges, Bananas", "Apples", "Bananas, Pears", "Bananas, Apples, Pears", "Pears"]
p @fruit.map{|f| f.split(', ') }.flatten.uniq
#=> ["Apples", "Oranges", "Bananas", "Pears"]
p @fruit.count{|f| f.include?("Apples")}
#=> 3
精彩评论