2012-05-23 75 views
1

我想不出如何將散列轉換爲多維數組。我從袋鼠產生以下哈希讀取Excel電子表格後:如何將散列轉換爲數組或數組?

{[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 

回答

1

如果你使用1.9+,那麼你可以利用有序哈希值的像這樣的東西:

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

如果」不確定哈希將按照正確的順序構建,那麼您可以強制執行「按座標排序」:

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"]] 
+0

感謝您的偉大的建議。我試過了,出現以下錯誤:未定義的方法'group_by'爲# user1411496

+0

@ user1411496:'spreadsheet'應該是您的哈希,而不是您的實際電子表格。 –

+0

電子表格是我的散列... input.default_sheet = input.sheets [0](來自roo)。我的哈希然後是「輸入」... – user1411496

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"]] 

由於畝太短的提示i可以重寫一點點:

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

你可能想要注意爲什麼你不落入通常的'Hash.new([])'陷阱。 –

+0

什麼是陷阱? – megas

+0

看看'[1] .each_with_object(Hash.new([])){| i,h | h [i] << i}',你應該看到它。 –