哈希的简单打印键?

时间:2021-03-12 10:46:26

I want to print a key from a given hash key but I can't find a simple solution:

我想从给定的哈希键打印一个键,但我找不到一个简单的解决方案:

myhash = Hash.new
myhash["a"] = "bar"

# not working
myhash.fetch("a"){|k|  puts k } 

# working, but ugly
if myhash.has_key("a")?
    puts "a"
end

Is there any other way?

还有别的办法吗?

3 个解决方案

#1


6  

I don't quite understand. If you already know you want to puts the value "a", then you only need to puts "a".

我不太明白。如果您已经知道要将值设置为“a”,那么您只需要输入“a”。

What would make sense would be to search for the key of a given value, like so:

有意义的是搜索给定值的键,如下所示:

puts myhash.key 'bar'
=> "a"

Or, if it's unknown whether the key exists in the hash or not, and you want to print it only if it exists:

或者,如果未知密钥是否存在于散列中,并且您希望仅在其存在时才打印它:

puts "a" if myhash.has_key? "a"

#2


13  

To get all the keys from a hash use the keys method:

要从哈希中获取所有密钥,请使用keys方法:

{ "1" => "foo", "2" => "bar" }.keys
=> ["1", "2"]

#3


10  

I know this is an older question, but I think what the original asker was intending was to find the key when he DOESN'T know what it is; For instance, when iterating through the hash.

我知道这是一个较老的问题,但我认为原始提问者想要的是在他不知道它是什么时找到钥匙;例如,在遍历哈希时。

A couple of other ways to get your hash key:

其他几种获取哈希键的方法:

Given the hash definition:

给定哈希定义:

myhash = Hash.new
myhash["a"] = "Hello, "
myhash["b"] = "World!"

The reason your first try didn't work:

你第一次尝试不起作用的原因:

#.fetch just returns the value at the given key UNLESS the key doesn't exist
#only then does the optional code block run.
myhash.fetch("a"){|k|  puts k } 
#=> "Hello!" (this is a returned value, NOT a screen output)
myhash.fetch("z"){|k|  puts k } 
#z  (this is a printed screen output from the puts command)
#=>nil   (this is the returned value)

So if you want to grab the key when iterating through the hash:

因此,如果您想在迭代哈希时获取密钥:

#when pulling each THEN the code block is always run on each result.
myhash.each_pair {|key,value| puts "#{key} = #{value}"}
#a = Hello, 
#b = World!

And if you're just into one-liners and want to:

如果你只是进入单行并希望:

Get the key for a given key (not sure why since you already know the key):

获取给定密钥的密钥(不知道为什么,因为您已经知道密钥):

myhash.each_key {|k| puts k if k == "a"}
#a

Get the key(s) for a given value:

获取给定值的密钥:

myhash.each_pair {|k,v| puts k if v == "Hello, "} 
#a

#1


6  

I don't quite understand. If you already know you want to puts the value "a", then you only need to puts "a".

我不太明白。如果您已经知道要将值设置为“a”,那么您只需要输入“a”。

What would make sense would be to search for the key of a given value, like so:

有意义的是搜索给定值的键,如下所示:

puts myhash.key 'bar'
=> "a"

Or, if it's unknown whether the key exists in the hash or not, and you want to print it only if it exists:

或者,如果未知密钥是否存在于散列中,并且您希望仅在其存在时才打印它:

puts "a" if myhash.has_key? "a"

#2


13  

To get all the keys from a hash use the keys method:

要从哈希中获取所有密钥,请使用keys方法:

{ "1" => "foo", "2" => "bar" }.keys
=> ["1", "2"]

#3


10  

I know this is an older question, but I think what the original asker was intending was to find the key when he DOESN'T know what it is; For instance, when iterating through the hash.

我知道这是一个较老的问题,但我认为原始提问者想要的是在他不知道它是什么时找到钥匙;例如,在遍历哈希时。

A couple of other ways to get your hash key:

其他几种获取哈希键的方法:

Given the hash definition:

给定哈希定义:

myhash = Hash.new
myhash["a"] = "Hello, "
myhash["b"] = "World!"

The reason your first try didn't work:

你第一次尝试不起作用的原因:

#.fetch just returns the value at the given key UNLESS the key doesn't exist
#only then does the optional code block run.
myhash.fetch("a"){|k|  puts k } 
#=> "Hello!" (this is a returned value, NOT a screen output)
myhash.fetch("z"){|k|  puts k } 
#z  (this is a printed screen output from the puts command)
#=>nil   (this is the returned value)

So if you want to grab the key when iterating through the hash:

因此,如果您想在迭代哈希时获取密钥:

#when pulling each THEN the code block is always run on each result.
myhash.each_pair {|key,value| puts "#{key} = #{value}"}
#a = Hello, 
#b = World!

And if you're just into one-liners and want to:

如果你只是进入单行并希望:

Get the key for a given key (not sure why since you already know the key):

获取给定密钥的密钥(不知道为什么,因为您已经知道密钥):

myhash.each_key {|k| puts k if k == "a"}
#a

Get the key(s) for a given value:

获取给定值的密钥:

myhash.each_pair {|k,v| puts k if v == "Hello, "} 
#a