I wrote a function that takes three arguments and assigns the data to 3 public variables. This is the code in .sol file

uint[] public rule1;
uint cond1;
string name1;

function setRule (string _name, uint _cond, uint[] _rule) public {

        for (uint i = 0; i < _rule.length; i++) {
            rule1[i] = _rule[i];
        name1 = _name;
        cond1 = _cond;

After compiling and running the contract in geth CLI. I am checking the following commands.


returns 0

token.setRule("my rule", 1, [1,2,3])

Tx is mined


returns 0


returns ""


returns 0

I want to assign the values passed in arguments to the public variables. How to do it?


Presumably the transaction fails, right? rule1 is of length 0, but you're trying to write to various indexes >= 0.

Try this instead:

for (uint i = 0; i < _rule.length; i++) {

push will extend the length of the array and add the item at the (new) end of the array.

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.