views:

107

answers:

1

So I've got this hash here, and I have a loop. When the loop detects that the hash already has a value vendor = "something" -- and the loop's value is "something" -- I want it to just go to the hash and update some values (price, visits).

To simplify this, basically I have an array of hashes, and when I find that I already have a hash with a certain vendor value, I want to update the hash (change just the price and visits).

For your reference, my catlists object looks something like this -- @catLists = {[1] => {:vendor, :price, :visits, :month, :type}, [2] => {:vendor, :price, :visits, :month, :type}, [3] => {:vendor, :price, :visits, :month, :type}, etc}

@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

How would I go about doing this?

Thanks for the help, sorry for the complexity of this problem. It's sort of weird I couldn't find a good way to do this.

+2  A: 

Why doesn't this work when you've found a hit?

@catLists.each do |item|
  if (item.has_value?(thevendor)) 
    hit = "yes"
    item[:price] = theprice
    item[:visits] = numvisits
  end
end
Terry Lorber
OH aha I wrote item[price] = item[price] + theprice and it didn't work (forgot the colon). thanks!
Sam