比较散列中的值并返回匹配的键

时间:2021-10-29 13:46:02

I'm trying to look through a hash and compare it's values to an existing string and then when the match is found I want to output its key. I trying to write this in a code block and output the result to the console.

我正在尝试查看散列并将其值与现有字符串进行比较,然后在找到匹配项时我想输出其密钥。我试图在代码块中写这个并将结果输出到控制台。

officer.name = "Dave"

@hash = { "Tom" => "97", "Dave" => "98", "John" => "99" }

@hash.each { |key, value| do
    if #{key} == officer.name
        puts "id: #{value}"
    else
        puts "no match"
    end
}

Right now my console outputs:

现在我的控制台输出:

 id: 97
 no match
 id: 98
 no match
 id: 99
 no match

I'm trying to get it to output just the value of #{value} based on it's matching #{key}, which in this case would be Dave. So for the above example I want my console to spit just the number 98 or "no match".

我试图让它只输出#{value}的值,基于匹配的#{key},在这种情况下是Dave。所以对于上面的例子,我希望我的控制台只吐出数字98或“不匹配”。

6 个解决方案

#1


5  

This is a hash! You can do what you attempt way more efficiently:

这是一个哈希!您可以更有效地执行您尝试的方式:

officer.name = "Dave"

@hash = { "Tom" => "97", "Dave" => "98", "John" => "99" }

unless @hash.key?(officer.name) 
  puts "no match"
else 
  puts "id: #{@hash[officer.name]}"
end

#2


2  

Is it because you forgot the " ?

是因为你忘记了“?

if "#{key}" == officer.name

but you could just do

但你可以做到

if key == officer.name

#3


1  

officer.name = "Dave"

@hash = { "Tom" => "97", "Dave" => "98", "John" => "99" }

@hash.each do |key, value|
  if key == officer.name
    puts key
  else
    puts "no match"
  end
end

This should work

这应该工作

#4


1  

@hash.has_key?(officer.name) ? @hash[officer.name] : "no match"

#5


0  

When doing hash lookups by key, avoid #[]. Favor #fetch instead:

按密钥进行哈希查找时,请避免使用#[]。赞成#fetch:

officer.name = "Dave"
@hash = { "Tom" => "97", "Dave" => "98", "John" => "99" }
puts @hash.fetch(officer.name, 'no match')

#fetch allows you to specify a default value other than nil, which will prevent unexpected nils from hash lookups from throwing the all-too-common NoMethodError.

#fetch允许您指定除nil之外的默认值,这将防止哈希查找的意外nils抛出非常常见的NoMethodError。

#6


0  

this works too. slight edit from original post. it prints out answer for each pair though and doesn't identify which pair the answer refers to.

这也有效。从原始帖子略微编辑。它打印出每对的答案,但不确定答案所指的对。

officer = "Dave"

@hash = { "Tom" => "97", "Dave" => "98", "John" => "99" }

@hash.each do |key, value|
    if key == officer
    puts key
  else
    puts "no match"
  end
end

#1


5  

This is a hash! You can do what you attempt way more efficiently:

这是一个哈希!您可以更有效地执行您尝试的方式:

officer.name = "Dave"

@hash = { "Tom" => "97", "Dave" => "98", "John" => "99" }

unless @hash.key?(officer.name) 
  puts "no match"
else 
  puts "id: #{@hash[officer.name]}"
end

#2


2  

Is it because you forgot the " ?

是因为你忘记了“?

if "#{key}" == officer.name

but you could just do

但你可以做到

if key == officer.name

#3


1  

officer.name = "Dave"

@hash = { "Tom" => "97", "Dave" => "98", "John" => "99" }

@hash.each do |key, value|
  if key == officer.name
    puts key
  else
    puts "no match"
  end
end

This should work

这应该工作

#4


1  

@hash.has_key?(officer.name) ? @hash[officer.name] : "no match"

#5


0  

When doing hash lookups by key, avoid #[]. Favor #fetch instead:

按密钥进行哈希查找时,请避免使用#[]。赞成#fetch:

officer.name = "Dave"
@hash = { "Tom" => "97", "Dave" => "98", "John" => "99" }
puts @hash.fetch(officer.name, 'no match')

#fetch allows you to specify a default value other than nil, which will prevent unexpected nils from hash lookups from throwing the all-too-common NoMethodError.

#fetch允许您指定除nil之外的默认值,这将防止哈希查找的意外nils抛出非常常见的NoMethodError。

#6


0  

this works too. slight edit from original post. it prints out answer for each pair though and doesn't identify which pair the answer refers to.

这也有效。从原始帖子略微编辑。它打印出每对的答案,但不确定答案所指的对。

officer = "Dave"

@hash = { "Tom" => "97", "Dave" => "98", "John" => "99" }

@hash.each do |key, value|
    if key == officer
    puts key
  else
    puts "no match"
  end
end