dosrmo0442 2014-11-26 17:28
浏览 41
已采纳

将PHP数组操作转换为Ruby哈希

I've written this script to be run in PHP, and am trying to convert it to Ruby.

Code:

$cases = array();


foreach($results as $result)
{
    if(!array_key_exists($result['id'], $cases))
        $cases[$result['id']] = array($result);
    else
        $cases[$result['id']][] = $result;
}

foreach($cases as $key => $case)
{
    foreach($case as $payment)
    {
        if(count($case) > 1)
        {

            $cases[$key]['total'] += ($payment['p1'] > $payment['p2']) ? $payment['p1'] : $payment['p2'];

        } else {

            $cases[$key]['total'] = ($payment['p1'] > $payment['p2']) ? $payment['p1'] : $payment['p2'];
        }
    }
}

In Ruby, I have the results as an array returned by MySQL (using mysql2 gem). Using the loop below, it prints every row. How can I check if the key exists when it does not provide keys?

What's the best way to implement this code in Ruby? How can I add a hash onto a hash in Ruby similar to adding an array onto an array in PHP.

results.each(:as => :array) do |row|
    puts row.inspect
end

More explanation:

PHP Structure

Array(
    0 => Array(
        Array(
            [id] => 'random id',
            [p1] => 534,
            [p2] => 105
        ),
        Array(
            [id] => 'random id',
            [p1] => 335,
            [p2] => 425
        )
    ),
    1 => Array(
        Array(
            [id] => 'random id',
            [p1] => 259,
            [p2] => 124
        )
    )
)

And i'm trying to iterate over each array inside the first, and if p1 > p2, add p1 to the total key on that array. i.e., the 0 index would look like this:

0 => Array(
    'total' => 959,
    Array(
        [id] => 'random id',
        [p1] => 534,
        [p2] => 105
    ),
    Array(
        [id] => 'random id',
        [p1] => 335,
        [p2] => 425
    )
)
  • 写回答

1条回答 默认 最新

  • duan2891 2014-11-26 17:32
    关注

    Are you trying to group by result['id']? I believe that "translating" your PHP code to ruby will look something like this:

    cases = {}
    results.each do |result|
      if cases[result['id']]
        cases[result['id']] << result
      else
        cases[result['id']] = [result]
      end
    end
    

    Another way to get the same result is to use group_by:

    cases = results.group_by { |result| result['id'] }
    

    The second part "translated" to ruby will look something like this:

    cases.each do |key, c|
      c.each do |payment|
        if c.count > 1 
          c['total'] += (payment['p1'] > payment['p2']) ? payment['p1'] : payment['p2']
        else
          c['total'] = (payment['p1'] > payment['p2']) ? payment['p1'] : payment['p2']
        end
      end
    end
    

    (this will actually not work in ruby, since an array cannot have an arbitrary key like 'total' - it accepts only numbers in its brackets. You will have to modify this code to hold the totals in a different structure)

    Again, a more idiomatic way of summing up the max between 'p1' and 'p2' of each payment in each case might look more like this:

    totals = cases.values.map do |c|
      c.inject(0) { |sum, payment| sum + [payment['p1'], payment['p2']].max }
    end
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧