如何将哈希转换为数组或数组?

时间:2012-05-23 01:46:32

标签: ruby multidimensional-array

我无法弄清楚如何将哈希转换为多维数组。在阅读Excel电子表格后,我从roo生成了以下哈希:

{[1, 1]=>"string-1", [1, 2]=>"string-2", [1, 3]=>"string-3", [1, 4]=>"string-4", 
[1, 5]=>"string-5", [1, 6]=>"string-6", [1, 7]=>"string-7", [2, 1]=>"string-1", 
[2, 2]=>"string-2", [2, 3]=>numeric-1, [2, 4]=>numeric-2, [2, 5]=>"string-3", 
[2, 6]=>"string-4", [2, 7]=>numeric-3, [3, 1]=>"string-1", [3, 2]=>"string-2", 
[3, 3]=>numeric-1, [3, 4]=>numeric-2, [3, 5]=>"string-3", [3, 6]=>"string-4", 
[3, 7]=>numeric-3, ... etc}

我需要将其转换为:

[["string-1", "string-2", "string-3", "string-4", "string-5", "string-6", "string-7"], 
["string-1", "string-2", numeric-1, numeric-2, "string-3", "string-4", numeric-3], 
["string-1", "string-2", numeric-1, numeric-2, "string-3", "string-4", numeric-3],
... etc]

我尝试了以下内容,但它只是将所有内容复制到一个数组中,而不是嵌入数组:

2.upto(input.last_row).each do |row|
  ((input.first_column)..(input.last_column)).map{ |col| 
    $rows << input.cell(row, col) if col != 5 and col <= 10}.join(" ")
end

我已经搜索了几个小时的答案,但找不到解决方案。

以下最终成为我可行的解决方案:

((xlsx.first_row)..(xlsx.last_row)).each do |row|
  ((xlsx.first_column)..(xlsx.last_column)).each do |col|
    $tmp_row << xlsx.cell(row, col) if col != 5 and col <= 10
  end
remove_newlines_from_strings($tmp_row)
$rows_sheet_0 << $tmp_row
$tmp_row = []
end

2 个答案:

答案 0 :(得分:1)

如果你使用的是1.9+,那么你可以利用这样的有序哈希:

a_of_as = spreadsheet.group_by { |k, v| k.first }.map { |k, v| v.map(&:last) }

如果您不确定哈希是否会以正确的顺序构建,那么您可以通过坐标强制进行排序&#34;像这样:

a_of_as = spreadsheet.sort_by  { |k, v| k }
                     .group_by { |k, v| k.first }
                     .map      { |k, v| v.map(&:last) }

我假设网格中没有任何空白,但在处理电子表格时这似乎是一个安全的假设。

例如(为紧凑性重新格式化):

>> pp spreadsheet
{[1, 1]=>"string-1", [1, 2]=>"string-2", [1, 3]=>"string-3",  [1, 4]=>"string-4",  [1, 5]=>"string-5", [1, 6]=>"string-6", [1, 7]=>"string-7",
 [2, 1]=>"string-1", [2, 2]=>"string-2", [2, 3]=>"numeric-1", [2, 4]=>"numeric-2", [2, 5]=>"string-3", [2, 6]=>"string-4", [2, 7]=>"numeric-3",
 [3, 1]=>"string-1", [3, 2]=>"string-2", [3, 3]=>"numeric-1", [3, 4]=>"numeric-2", [3, 5]=>"string-3", [3, 6]=>"string-4", [3, 7]=>"numeric-3"}

>> pp spreadsheet.group_by { |k, v| k.first }.map { |k, v| v.map(&:last) }
[["string-1", "string-2", "string-3",  "string-4",  "string-5", "string-6", "string-7"],
 ["string-1", "string-2", "numeric-1", "numeric-2", "string-3", "string-4", "numeric-3"],
 ["string-1", "string-2", "numeric-1", "numeric-2", "string-3", "string-4", "numeric-3"]]

>> spreadsheet2 = Hash[spreadsheet.sort { |(ka,va),(kb,vb)| kb <=> ka }]
>> pp spreadsheet2
{[3, 7]=>"numeric-3", [3, 6]=>"string-4", [3, 5]=>"string-3", [3, 4]=>"numeric-2", [3, 3]=>"numeric-1", [3, 2]=>"string-2", [3, 1]=>"string-1", 
 [2, 7]=>"numeric-3", [2, 6]=>"string-4", [2, 5]=>"string-3", [2, 4]=>"numeric-2", [2, 3]=>"numeric-1", [2, 2]=>"string-2", [2, 1]=>"string-1",
 [1, 7]=>"string-7",  [1, 6]=>"string-6", [1, 5]=>"string-5", [1, 4]=>"string-4",  [1, 3]=>"string-3",  [1, 2]=>"string-2",  [1, 1]=>"string-1"}

>> pp spreadsheet2.sort_by { |k,v| k }.group_by { |k, v| k.first }.map { |k, v| v.map(&:last) }
[["string-1", "string-2", "string-3",  "string-4",  "string-5", "string-6", "string-7"],
 ["string-1", "string-2", "numeric-1", "numeric-2", "string-3", "string-4", "numeric-3"],
 ["string-1", "string-2", "numeric-1", "numeric-2", "string-3", "string-4", "numeric-3"]]

答案 1 :(得分:1)

h = {[1, 1]=>"string-1",[1, 2]=>"string-2",[1, 3]=>"string-3",[1, 4]=>"string-4",
[1,5]=>"string-5",[1, 6]=>"string-6",[1, 7]=>"string-7",[2, 1]=>"string-1",
[2, 2]=>"string-2",[2, 3]=>"numeric-1",[2, 4]=>"numeric-2",[2, 5]=>"string-3",
[2, 6]=>"string-4",[2, 7]=>"numeric-3",[3, 1]=>"string-1",[3, 2]=>"string-2",
[3, 3]=>"numeric-1",[3, 4]=>"numeric-2",[3, 5]=>"string-3",[3, 6]=>"string-4",
[3, 7]=>"numeric-3"}

h.each_with_object(Hash.new([])){ |m,res| res[m.first.first] += [m.last] }.values

#=>[["string-1","string-2","string-3","string-4","string-5","string-6","string-7"],
#=> ["string-1","string-2","numeric-1","numeric-2","string-3","string-4","numeric-3"],
#=> ["string-1","string-2","numeric-1","numeric-2","string-3","string-4","numeric-3"]]

感谢 mu太短提示我可以稍微改写一下:

h.each_with_object(Hash.new{|h,k|h[k]=[]}) do |m,res| 
  res[m.first.first] << m.last 
end.values