46

Here's my function:

function Test() constant returns (uint[]) {
   var myArray = uint[];
   myArray.push(123); 
   return myArray;
}

Here's an error I get in the solidity online compiler:

ballot.sol:25:9: Error: Member "push" not found or not visible after argument-dependent lookup in type(uint256[] memory) myArray.push(123);

eth
  • 85,679
  • 53
  • 285
  • 406
manidos
  • 4,298
  • 3
  • 31
  • 55

2 Answers2

66

You need to use an array in storage because it is not possible to resize memory arrays.

contract D { 
    uint[] myArray;
    function Test() constant returns (uint[]) {
       myArray.push(123); 
       return myArray;
    }
}

Otherwise you need to define the size of the array during initialization:

contract C {
    function f(uint len) {
        uint[] memory a = new uint[](7);
        bytes memory b = new bytes(len);
        // Here we have a.length == 7 and b.length == len
        a[6] = 8;
    }
}
eth
  • 85,679
  • 53
  • 285
  • 406
4

You could define the array with the length of the array you want to filter. Problem is all the tail of empty elements.

uint[] memory filteredArray = new uint[](fullArray.length);
a[0] = 1;
R01010010
  • 281
  • 2
  • 5