簡體   English   中英

Ruby使用主鍵和輔助鍵進行哈希處理

[英]Ruby hashes with a primary and secondary key

我從RabbitMQ隊列中讀取的數據具有以下值: epoch, id, wait, length, max 我想把這些信息放入哈希哈希中。 在Perl中,我會這樣做:

%hash = ( $spid => { $epoch => { 'wait' => $wait, 'length' => $length, 'max' => $max } } );

這個帖子似乎很接近,但我要么不理解回復,要么就是我不想要的。 在學習如何在Ruby中執行此操作時,將非常感謝任何幫助。

散列哈希:

mainHash = {}
h1 = { 'test' => 1, 'test2' => 2}
h2 = { 'asdf' => 3 }
mainHash[:h1] = h1
mainHash[:h2] = h2

要訪問'asdf'值:

mainHash[:h2]['asdf']

如何將數據放入哈希值取決於數據。

沒有太大區別:

epoch, id, wait, length, max = 1, 2, 3, 4, 5
hash = {id => {epoch => {'wait' => wait, 'length' => length, 'max' => max}}}
p hash                 # => {2=>{1=>{"wait"=>3, "length"=>4, "max"=>5}}}
puts hash[2][1]["max"] # => 5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM