ruby - Incrementing value in a hash -
this question has answer here:
- adding value in hash table 1 answer
i want increment value of first item one. not understand why code not working
puts sales_hash sales_hash.values[0] = sales_hash.values[0] +1 puts sales_hash
i adding 1 first value in sales_hash
. when print out sales_hash
first value remains same. doing wrong?
when use hash#values
receive array contains copy of values in hash. therefor, assignment not change hash rather array containing values.
moreover, should aware elements in hash ordered order of key's insertion, might cause unexpected results if access "position" in hash since insertions , deletions may change order of hash.
if want update value in hash, should access using it's key, example: sales_hash[:my_sale] = sales_hash[:my_sale] +1
Comments
Post a Comment