【问题标题】:Ruby hashes with a primary and secondary key具有主键和辅助键的 Ruby 哈希
【发布时间】:2013-05-26 07:47:17
【问题描述】:

我从 RabbitMQ 队列中读取的数据具有以下值:epoch, id, wait, length, max。我想将此信息放入哈希哈希中。在 Perl 中我会这样做:

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

This thread 似乎很接近,但我要么不理解回复,要么不是我想要的。在学习如何在 Ruby 中执行此操作时,我们将不胜感激。

【问题讨论】:

标签: ruby arrays perl hash


【解决方案1】:

差别不大:

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

【讨论】:

    【解决方案2】:

    哈希散列:

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

    访问“asdf”值:

    mainHash[:h2]['asdf']
    

    如何将数据放入哈希中取决于数据。

    【讨论】:

      猜你喜欢
      • 2011-06-09
      • 2017-03-31
      • 1970-01-01
      • 2011-07-01
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2019-04-24
      相关资源
      最近更新 更多