3

What does setting the length property of an array do? In most other languages length is a function that return the length of an array.

I have the following code:

struct ledger_t {
  uint8 WinningNum1;
  uint8 WinningNum2;
  uint8 WinningNum3;
  uint8 WinningNum4;
  bytes32 ClosingHash;
  bytes32 OpeningHash;
  mapping(address=>bet_t) bets;
  uint Guess4OutOf4;
  uint Guess3OutOf4;
  uint Guess2OutOf4;
  uint Guess1OutOf4;
  uint PriceOfTicket;
  uint ExpirationTime; //for eventual refunds only, ~2 weeks after draw announced
}
ledger_t[] public ledger;

//constructor
function TheEthereumLottery() {
  owner = msg.sender;
  ledger.length = 0;
  IndexOfCurrentDraw = 0;
  referral_fee = 90;
}

1 Answer 1

4

What does setting the length property of an array do.

It sets the length of the array.

The proviso is that the array must be dynamic, and must be a storage rather than memory variable.

From the documentation:

You can resize a dynamic array in storage (i.e. an array declared at the contract level) with arrayname.length = <some new length>;. If you get the “lvalue” error, you are probably doing one of two things wrong.

  • You might be trying to resize an array in “memory”, or
  • You might be trying to resize a non-dynamic array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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