2009-07-21 80 views
0

所以我在這裏有這個散列,我有一個循環。當循環檢測到hash已經有一個值vendor =「something」 - 並且循環的值是「something」時 - 我希望它只是去散列並更新一些值(價格,訪問)。更新散列

爲了簡化這個,基本上我有一個散列數組,當我發現我已經有一個具有某個供應商值的散列時,我想更新散列(只更改價格和訪問)。我的catlists對象看起來像這樣 - @catLists = {[1] => {:vendor,:price,:visits,:month,:type},[2] => {:供應商:價格,:參觀,:月:類型},[3] => {:供應商:價格,:參觀,:月:類型}等}

@income.each do |query| 
      queriedmonth = Date::MONTHNAMES[query.postdate.month] 
      thevendor = query.detail.capitalize 
      theprice = query.amount 
      numvisits = 1 #change this later 
      hit = "no" 

      @catLists.each do |item| 
       if (item.has_value?(thevendor)) 
        hit = "yes" 
        //So here I want to update the hash in my catLists array. 
       end 
      end 
      if (hit=="no") 
       vendorlist << thevendor 
       @catList = {:vendor => thevendor, :price => theprice, :visits => numvisits, :month =>queriedmonth, :type=> "income"} 
       #Push onto our array 
       @catLists << @catList 
      end 


     end 

我怎麼會去關於這樣做?

感謝您的幫助,對於這個問題的複雜性感到抱歉。這有點奇怪,我找不到這樣做的好方法。

回答

2

爲什麼當你找到一個命中時這不工作?

@catLists.each do |item| 
    if (item.has_value?(thevendor)) 
    hit = "yes" 
    item[:price] = theprice 
    item[:visits] = numvisits 
    end 
end 
+0

OH aha我寫了item [price] = item [price] +價格,它沒有工作(忘了冒號)。謝謝! – Sam 2009-07-21 01:34:27