2017-08-07 143 views
0

我有這些散列作爲輸出,我需要推入數組而不覆蓋。將哈希推入數組

output1= {:user_id=>9, :project_id=>4, :task_id=>87, :comment=>"Test 20"} 
output2 = {:user_id=>9, :project_id=>12, :task_id=>105,:comment=>"Test 21"} 

我需要推動這2輸出到一個單一的數組,當我遍歷循環。現在發生的事情是,當我將第二個輸出插入到數組中時,它會覆蓋第一個輸出,下面是我得到的結果。

Entry_array=[{:user_id=>9,:project_id=>12,:task_id=>105,:comment=>"Test 21"}, 
{:user_id=>9, :project_id=>12, :task_id=>105,:comment=>"Test 21"}] 

我想要結合散列輸出1和散列輸出2的結果。 非常感謝所有的幫助。

這是我使用

attributes =[:user_id,:project_id,task_id,:comment] 
    entry_array=[] 
    output = {} 

    CSV.foreach(csv_file, headers: true, converters: :date).with_index do |row,line_no| 
    entry_hash= row.to_hash 
    .....some code here where we get the entry_hash.... 
    i=0 

    entry_array <<output 
    end 
+1

請顯示一些代碼(例如,在'循環遍歷'的位置)。 – jvillian

+1

我認爲它需要更多一點的代碼。「我」定義在哪裏?什麼是'entry_hash'?什麼是「輸出」?什麼是「屬性」? –

回答

2

出現這種情況的原因,至少根據你的代碼是因爲你使用了相同的哈希output每一行。如果你運行了

puts entry_array.collect(&:object_id) 

在你的CSV文件末尾,你會看到它們都是同一個對象。所以,即使您將其放入每行末尾的數組中,仍然正在修改陣列現在指向的同一個對象。基本上你在做什麼是

a = { hello: 'world' } # => {:hello=>"world"} 
b = a     # => {:hello=>"world"} 
b[:hello] = 'there' 
a      # => {:hello=>"there"} 
b      # => {:hello=>"there"} 

# storing it in an array does the same thing 
output = { hello: 'world' } # => {:hello=>"world"} 
array = [output]   # => [{:hello=>"world"}] 
output[:hello] = 'there' 
output      # => {:hello=>"there"} 
array      # => [{:hello=>"there"}] 

你需要做修復這是什麼實例化一個新的哈希每一行:

attributes = [:user_id, :project_id, :task_id, :comment] 
entry_array = [] 

CSV.foreach(csv_file, headers: true, converters: :date).with_index do |row, line_no| 
    output = { } # Instantiate here, inside the loop, so each row gets its own hash 
    entry_hash = row.to_hash 

    # if you need the key for something, use this 
    # entry_hash.each.with_index do |(key, value), index| 
    # otherwise, just iterate over each value 
    entry_hash.each_value.with_index do |value, index| 
    output[attributes[index]] = value.is_a?(Array) ? value.first.to_i : value 
    end 

    entry_array << output 
end 

我已經改變了你的類支票。is_a?也刪除了i計數器,贊成在迭代中使用with_index,在示例中沒有使用key,所以我剛使用each_value,但留下了一條評論,說明如何在散列上使用each_indexeachkey只是沒有顯示。

+0

非常感謝!有效 – Archie123

0

我不知道這是爲了讓你夢想成真最好的方式,但它是一種

1)創建數組

arr_of_hashes = []

2)現在,你有你的數組您可以與您的哈希值來填充它

output1= {:user_id=>9, :project_id=>4, :task_id=>87, :comment=>"Test 20"} 
output2 = {:user_id=>9, :project_id=>12, :task_id=>105,:comment=>"Test 21"} 

arr_of_hashes << output1 
arr_of_hashes << output2 
... 

3)現在當您檢查您的arr_of_hashes的價值,你會得到

[{:user_id=>9, :project_id=>4, :task_id=>87, :comment=>"Test 20"}, {:user_id=>9, :project_id=>12, :task_id=>105, :comment=>"Test 21"}]

我希望這有助於:)

快樂黑客

CSV.foreach(csv_file, headers: true, converters: :date).with_index do |row,line_no| 

.....some code here where we get the entry_hash.... 

    entry_array = [] # you need to define the element first before you can add stuff to it :) 
    entry_hash.each do |key,value| 
     if value.class == Array 
      output[attributes[i]] = value.first.to_i 
     else 
      output[attributes[i]] = value 
     end 
     i += 1 
    end 
    entry_array <<output 
    end