【问题标题】:Updating a Hash更新哈希
【发布时间】:2009-07-21 00:38:19
【问题描述】:

所以我这里有这个哈希,我有一个循环。当循环检测到散列已经有一个值 vendor = "something" - 并且循环的值是 "something" - 我希望它只是去散列并更新一些值(价格,访问)。

为了简化这一点,基本上我有一个哈希数组,当我发现我已经有一个具有特定供应商值的哈希时,我想更新哈希(只更改价格和访问量)。

供您参考,我的 catlists 对象看起来像这样 -- @catLists = {[1] => {:vendor, :price, :visits, :month, :type}, [2] => {:vendor , :price, :visits, :month, :type}, [3] => {:vendor, :price, :visits, :month, :type} 等}

@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

我该怎么做呢?

感谢您的帮助,对于这个问题的复杂性,我们深表歉意。有点奇怪,我找不到这样做的好方法。

【问题讨论】:

    标签: ruby-on-rails ruby arrays


    【解决方案1】:

    为什么当你找到一个热门时这不起作用?

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

    【讨论】:

    • 哦啊哈,我写了 item[price] = item[price] + theprice,但它不起作用(忘记了冒号)。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 2019-10-28
    • 2021-01-22
    相关资源
    最近更新 更多