0

I need to add a number to existing value in array under index 'id'. I have the following code:

 contract Store {

  uint256[] public totalamt;

  function append(uint256 id,uint price) payable{

    totalamt[id] = totalamt[id] + price;

}

   }

Array is not initialized.Each time I need to update the array.Help will be appreciated :)

2

I think you're better off with a mapping. It does exactly what you want.

Here's how:

contract Store {

    mapping(uint256 => uint256) public totalamt;

    function append(uint256 id,uint price) payable public {

        totalamt[id] = totalamt[id] + price;

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.