8

In Solidity, you can increase the size of an array to make room for a new member by using array.length++. But I'm getting an error:

Value must be an lvalue
Sebi
  • 4,262
  • 13
  • 60
  • 116
fivedogit
  • 8,374
  • 7
  • 34
  • 43

2 Answers2

10

You can resize a dynamic array in storage (i.e. an array declared at the contract level) with “arrayname.length = ;” But 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.

int8[] memory somearray;     // CASE 1
somearray.length++;          // illegal 

int8[5] somearray;           // CASE 2
somearray.length++;          // illegal 

IMPORTANT NOTE: In Solidity, arrays are declared backwards from the way you’re probably used to declaring them. And if you have a >=2D array with some dynamic and some non-dynamic components, you may violate #2 and not understand why. Note also that arrays are accessed the “normal” way. Here's are some examples of this "backward" declaration paradigm in action:

int8[][5] somearray;  // This is 5 dyn arrays, NOT a dyn array-of-arrays w/len=5
// so...
somearray[4];         // the last dynamic array
somearray[1][12];     // the 13th element of the second dynamic array
// thus...
somearray.length++;   // illegal. This array has length 5. Always.
somearray[0].length++;// legal
fivedogit
  • 8,374
  • 7
  • 34
  • 43
  • what if my variable length array is just temporary and should remain as memory? i can't modify the length or push a new item to it? – okwme Aug 20 '17 at 09:28
0

Encountered same issue and what I had to was use the storage keyword since I was trying to modify a global storage array.

bytes32[] storage someArray = someGlobalStorageArray;
Miguel Mota
  • 20,135
  • 5
  • 45
  • 64