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
)
)