Ruby 初心者スレッド Part 24
at TECH
806:デフォルトの名無しさん
09/01/25 20:03:10
>>805
配列要素をeachで枚挙することを厭ってはいけない
特にRubyではね
words = ["apple", "orange", "lemon"]
sentence = ["apple is red","orange is not blue","lemon is yellow","apple is not orange","lemon is not red"]
counts = {"apple"=>0, "orange"=>0, "lemon"=>0}
sentence.each do |s|
words.each do |word|
if /#{word}/ =~ s then
counts[word] = counts[word]+1
end
end
end
p counts
結果:
{"orange"=>2, "apple"=>2, "lemon"=>2}
次ページ続きを表示1を表示最新レス表示スレッドの検索類似スレ一覧話題のニュースおまかせリスト▼オプションを表示暇つぶし2ch
5503日前に更新/246 KB
担当:undef