将多个值附加到空哈希中的一个键
Appending multiple values to one key in an empty hash
我正在尝试在数组中查找与文件中字典单词匹配最多的字符串。我将分数(匹配项)存储为哈希的键,并将相应的匹配字符串存储为键的值。例如:
字符串 "XXBUTTYATCATYSSX"
有三个子字符串单词匹配。该字符串的得分为 3
。字符串和分数存储在 scores
散列中:
scores = { 3 => "XXBUTTYATCATYSSX" }
字符串"YOUKKYUHISJFXPOP"
也有3个匹配项。这应该存储在散列中:
scores = { 3 => "XXBUTTYATCATYSSX", "YOUKKYUHISJFXPOP" }
scores = { }
#scores = Hash.new { |hash, key| hash[key] = [] }
File.open("#{File.dirname(__FILE__)}/dictionary.txt","r") do |file|
#going to a string in the array
strArray.each_index do |str|
score = 0
match = strArray[str]
#going to a line in the dictionary file
file.each_line do |line|
dictWord = line.strip!.upcase
if match.include? dictWord
score += 1
end
end
#the key in the scores hash equals the score (amount of matches)
#the values in the scores hash are the matched strings that have the score of the key
#scores[score] << match
scores.merge!("#{score}" => match)
end
编辑:
我修改了上面的代码。现在第一次循环后不会进入file.each_line do |line|
请帮忙。
对于 File
个对象,您无法读取它们两次。也就是说,如果您使用 each_line
读取整个文件一次,然后您尝试再次读取,第二次将不会执行任何操作,因为它已经在文件末尾。要再次读取文件,您需要在尝试读取文件之前用 file.rewind
倒带它。
第二个问题是您试图添加到一个不存在的数组中。例如:
scores = {}
scores[3] #=> nil
scores[3] << 'ASDASDASD' # crashes (can't use << with nil)
您需要为每个分数创建一个数组,然后才能向其中添加单词。一种方法是在使用之前检查密钥是否存在,如下所示:
scores = {}
if scores[3].nil?
scores[3] = []
end
scores[3] << 'word' # this will work
直接上代码:
scores = Hash.new
File.open("#{File.dirname(__FILE__)}/dictionary.txt","r") do |file|
strings.each do |string|
score = 0
file.each do |line|
score += 1 if string.match(line.strip!.upcase)
end
# store score and new array unless it already have same score
scores.store(score, []) unless scores.has_key?(score)
scores[score] << string
# rewind to read dictionary from first line on next iteration
file.rewind
end
end
strings
是要与字典进行比较的字符串数组:
例如strings = ["XXBUTTYYOUATCATYSSX", "YOUKKYUHISJFXPOP"]
)
我正在尝试在数组中查找与文件中字典单词匹配最多的字符串。我将分数(匹配项)存储为哈希的键,并将相应的匹配字符串存储为键的值。例如:
字符串
"XXBUTTYATCATYSSX"
有三个子字符串单词匹配。该字符串的得分为3
。字符串和分数存储在scores
散列中:scores = { 3 => "XXBUTTYATCATYSSX" }
字符串
"YOUKKYUHISJFXPOP"
也有3个匹配项。这应该存储在散列中:scores = { 3 => "XXBUTTYATCATYSSX", "YOUKKYUHISJFXPOP" }
scores = { }
#scores = Hash.new { |hash, key| hash[key] = [] }
File.open("#{File.dirname(__FILE__)}/dictionary.txt","r") do |file|
#going to a string in the array
strArray.each_index do |str|
score = 0
match = strArray[str]
#going to a line in the dictionary file
file.each_line do |line|
dictWord = line.strip!.upcase
if match.include? dictWord
score += 1
end
end
#the key in the scores hash equals the score (amount of matches)
#the values in the scores hash are the matched strings that have the score of the key
#scores[score] << match
scores.merge!("#{score}" => match)
end
编辑:
我修改了上面的代码。现在第一次循环后不会进入file.each_line do |line|
请帮忙。
对于 File
个对象,您无法读取它们两次。也就是说,如果您使用 each_line
读取整个文件一次,然后您尝试再次读取,第二次将不会执行任何操作,因为它已经在文件末尾。要再次读取文件,您需要在尝试读取文件之前用 file.rewind
倒带它。
第二个问题是您试图添加到一个不存在的数组中。例如:
scores = {}
scores[3] #=> nil
scores[3] << 'ASDASDASD' # crashes (can't use << with nil)
您需要为每个分数创建一个数组,然后才能向其中添加单词。一种方法是在使用之前检查密钥是否存在,如下所示:
scores = {}
if scores[3].nil?
scores[3] = []
end
scores[3] << 'word' # this will work
直接上代码:
scores = Hash.new
File.open("#{File.dirname(__FILE__)}/dictionary.txt","r") do |file|
strings.each do |string|
score = 0
file.each do |line|
score += 1 if string.match(line.strip!.upcase)
end
# store score and new array unless it already have same score
scores.store(score, []) unless scores.has_key?(score)
scores[score] << string
# rewind to read dictionary from first line on next iteration
file.rewind
end
end
strings
是要与字典进行比较的字符串数组:
例如strings = ["XXBUTTYYOUATCATYSSX", "YOUKKYUHISJFXPOP"]
)