Joining an array of keys to a hash with key value pairs like excel vlookup
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've got an unsorted array of keys like this: keys = ["ccc", "ddd", "ggg", "aaa", "bbb"] and a hash hash = {"ddd" => 4, "aaa" => 1, "bbb" => 2, "eee" => 5, "fff" => 6} I'd like to join these two data structures to return a hash in the original order of keys to the first keys: {"ccc" => nil, "ddd" => 4, "ggg" => nil, "aaa" => 1, "bbb" => 2} Items NOT in the hash (like "ggg") should return nil. This is analogous to the "v-lookup" function in excel. this is in ruby. Thanks! 回答1: Cryptic: Hash[keys.zip(hash.values_at *keys)] Or a bit longer, a