2009-07-21 38 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