简体   繁体   English

在一个哈希中将红宝石哈希中的键值分组并打印特定值

[英]Group values of keys in ruby hash in one hash and print specific value

This is my hash: 这是我的哈希:

{"Specialty"=>"Other (note in description);Medical;Dental;Vision", "Value"=>"https://www.example.com/ca"}
{"Value"=>"P.O. BOX 60007 LOS ANGELES, CA 90060"}
{"Specialty"=>"Pharmacy;Medical", "Value"=>"800-824-0898"}
{"Specialty"=>"Urgent Care;Medical", "Value"=>"800-700-9186"}
{"Specialty"=>"Urgent Care;Emergency Care;Medical;Chiropractor;Acupuncture", "Value"=>"800-677-6669"}
{"Specialty"=>"Pharmacy;Medical", "Value"=>"https://www.example.com"}
{"Specialty"=>"Claims", "Value"=>"https://www.example.com/consumer/claims/claimsoverview"}
{"Specialty"=>"Urgent Care;Emergency Care;Medical;Chiropractor;Acupuncture", "Value"=>"https://example/example/apps/services/www/ABCBS/mobilewebapp/default/index.html#/providers/search"}
{""Specialty"=>"Claims;Customer Service;Medical", "Value"=>"800-888-8288"}

You can see from this that the key value has differed values in each hash table, and I would like to print only one specific value, that is PO BOX 60007 LOS ANGELES, CA 90060 你由此可以看出,关键value已经相差values在每个哈希表,我想只打印一个特定的值,也就是PO BOX 60007 LOS ANGELES, CA 90060

I tried printing the values of the hash by hash["value"] and it prints all the contents of the values. 我尝试通过hash["value"]打印hash["value"] ,并打印所有值。 I tried loading them into an array[] by using to_s function to print the value I want, but one array is loaded with all these values or I may be loading the array wrong. 我尝试使用to_s函数将它们加载到array[]以打印所需的值,但是所有这些值均已加载到一个数组中,否则我可能将数组加载错了。

array = []
array = medical.split("\n")
text = array.to_s
text1 = array.index{|s| s.include?("P.O.")}
puts text1

Can someone help me with how do I print that specific hash value inspite of various values existing in the key named value 有人可以帮助我如何打印特定的哈希值,尽管键名为value的键中存在各种value

If that's the pattern of the hash specifically, uniqueness I can figure out is that all hashes have multiple keys except the one you are looking for..considering it as an array of hashes 如果这正是hash的模式,那么我可以确定的唯一性是,除了要查找的哈希以外,所有哈希都具有多个键..将其视为哈希数组

hashes.each{ |hash| break hash['Value'] if hash.keys == ['Value'] }

UPDATE 更新

From the comments, instead of printing the hashes individually you can check and print the required value directly.. 从注释中,您可以直接检查并打印所需的值,而不必单独打印散列。

results1.map{ |result1| (value = result1['Value']).include?('P.O. BOX') ? value : nil }.compact

It will return array of strings having substring PO BOX 它将返回具有子字符串PO BOX的字符串数组

Assuming your have an array of hashes and you want to search for 'PO' strings then you can do this: 假设您有一个哈希表数组,并且想要搜索“ PO”字符串,则可以执行以下操作:

arr.select { |hash| hash["Value"][/P\.O\./] }.flat_map(&:values)
#=> ["P.O. BOX 60007 LOS ANGELES, CA 90060"]
array_hashes = [
    {"Specialty"=>"Other (note in description);Medical;Dental;Vision", "Value"=>"https://www.example.com/ca"},
    {"Value"=>"P.O. BOX 60007 LOS ANGELES, CA 90060"},
    {"Specialty"=>"Pharmacy;Medical", "Value"=>"800-824-0898"},
    {"Specialty"=>"Urgent Care;Medical", "Value"=>"800-700-9186"},
    {"Specialty"=>"Urgent Care;Emergency Care;Medical;Chiropractor;Acupuncture", "Value"=>"800-677-6669"},
    {"Specialty"=>"Pharmacy;Medical", "Value"=>"https://www.example.com"},
    {"Specialty"=>"Claims", "Value"=>"https://www.example.com/consumer/claims/claimsoverview"},
    {"Specialty"=>"Urgent Care;Emergency Care;Medical;Chiropractor;Acupuncture", "Value"=>"https://example/example/apps/services/www/ABCBS/mobilewebapp/default/index.html#/providers/search"},
    {"Specialty"=>"Claims;Customer Service;Medical", "Value" => "800-888-8288"}] 

> array_hashes.select{|h| h["Value"].include?("P.O.")}.flat_map(&:values).first
#=> "P.O. BOX 60007 LOS ANGELES, CA 90060"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM