1

I'm trying to follow the tutorial here and there's a line where an address array players has this done players.length = 0 which issues this error: member length is read-only and cannot be used to resize arrays. I get that only pop and push are allowed as well as assigning a new empty array. I was wondering how to perform that last operation in this context.

1

players.length=0 was allowed before the solidity 0.6.0. After the update length is now read-only member.

There is a delete keyword you can use to delete the players array.

delete players

But the delete should never be use to remove a single element from the array. Since delete leave the gap between the array.

You can see first example over here.

contract MyContract {
  uint[] array = [1,2,3];

  function removeAtIndex(uint index) returns (uint[]) {
    if (index >= array.length) return;

    for (uint i = index; i < array.length-1; i++) {
      array[i] = array[i+1];
    }

    delete array[array.length-1];
    array.length--;

    return array;
  }
}
| improve this answer | |
  • 1
    You can use array.pop() to delete the last element in an array. – Ismael Jul 12 at 17:29

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