Questions tagged [arrays]
Questions regarding the use of Arrays in solidity.
386
questions
1
vote
1answer
103 views
What does State Trie save when pushing into array?
I'm interested/worried about the size of the State Trie over time
Lets suppose I have a contract with two vars:
uint myNumber;
uint[] myArray;
As far as I understand, If I change the value of "...
2
votes
1answer
19 views
Is it possible to return a table of values in solidity?
I am writing a code to return multiple values from two arrays simultaneously. I understand since I mentioned only one uint and one address as arguments in returns, the for loop is throwing an error. ...
1
vote
0answers
21 views
Solidity. Pushing a struct to an array. But that struct contains an array. Copying of type struct memory[] memory to storage not yet supported
Here is my code. I need to have a "checkpoint" object that stores some information including an array of "delegations" which is another object.
struct delegation{
uint256 ...
1
vote
3answers
1k views
Unable to return address array in solidity
I have the following code for a simple gambling game.
The code works fine except for getPlayers function
pragma solidity >=0.4.22 <0.6.0;
contract Lottery {
address payable public manager;
...
1
vote
1answer
24 views
Storing nested array (2D array)
So I am making an ERC1155 token (for the batch mint pretty much). It is only ever going to mint NFT's (as you will see now now).
I am trying to batch mint tokens and store an array for each one. So ...
0
votes
1answer
33 views
Adding element to mapping (solidity) is not working, even if the method is actually called in the transaction
I'm currently working (as a begineer) at a project involving ethereum blockchain and solidity.
In my contract I have a mapping with addresses as key and the value of an array of a FileDetails ...
0
votes
0answers
21 views
invalid opcode while getting an element from a string of array
I am having some problems executing the final function of this contract (getmsg)
this is the code:
struct req{
uint yes;
}
struct fri{
uint yes;
...
1
vote
1answer
11 views
How to empty uint array after value is assigned?
Here is an uint array defined in smart contract:
uint256[] public bidderItemID;
And bidderItemID is assigned value in contract:
bidderItemID = [1,2,3];
How can I empty bidderItemID after it is ...
1
vote
3answers
312 views
How do i add to a nested array within a struct
I am unable to add elements to a array within the struct
struct D {
string name;
address key;
}
struct Aone {
string aid;
B[] blist;
D[] dlist;
}
...
0
votes
2answers
206 views
Arrays copy memory in Solidity
I have this simple code:
function restart() public returns (uint256[] ) {
uint256[] memory result = new uint256[](5);
result[0] = 10;
uint256[] memory result1 = new uint256[](5);
...
1
vote
1answer
371 views
solidity sorting array - LIFO / FIFO
I am adding to an bytes32[] array via push().
When I traverse the array with for (uint i = 0; i < array.length; i++), This is First in First Out (FIFO) sequence right?
How can one perform a ...
3
votes
3answers
996 views
How to handle dynamic size string array in solidity?
I have a shop owner and a shop owner can have multiple products. I want to keep the products id in an array.
Also, A public method can return all the product id list of a shop owner.
I tried the ...
0
votes
1answer
19 views
Solidity: How to do 2D mapping?/Syntax Error
I am getting a syntax error. I am not sure if its a problem related to 2D mapping or 2D array. My code is given below:
contract Stealing{
mapping (address => uint) balances;
mapping (...
3
votes
3answers
4k views
Efficient approach to delete element from array in Solidity
For each user, I want to keep an array of holded assets (each asset has an ID).
My solution up until now is :
struct User {
uint userId;
uint[] assets;
}
For every asset the ...
0
votes
1answer
403 views
Setting values of an byte array dynamically
If I set the values of an array as follows, am I wrong?
uint256 n = 0;
bytes32[n] example;
function getArraySize(uint256 arraySize) public returns(uint256 size) {
size = arraySize;
return size;
}...
8
votes
3answers
8k views
How to initialize an empty array inside a struct?
Is there a mistake in the way I initialize Bar b in the function foobar?
contract Foo {
struct Bar {
address owner;
uint[] x;
}
Bar[] public bars;
function foobar(...
0
votes
1answer
13 views
Increase values in array
I am increasing the value at a particular index in Solidity but it is giving me bad instruction error.
Can anyone tell me what I am missing here?
Please check the sample contract for reference:
pragma ...
0
votes
1answer
87 views
How do i loop through an array of structs? “TypeError: Integer constant expected.” Solidity
What i want my code to do
Everytime the function is called the exampleValue of all users should be increased by 5
What the code actually does
When i compile it in Remix i get this error message:
...
0
votes
1answer
18 views
array problem invalid opcode
new to ethereum. I'm tring to write smartcontracto to manage car rental. Can anyone explain me why the function to insert new car is not working (code below):
function insertnewcar(string memory model,...
0
votes
2answers
187 views
Remove array specific value
In a array that the values are never repeated. Is there a function/library or something similar that you give the value to remove. And the function the empty space in the array??
function remove(uint ...
0
votes
1answer
23 views
Returning a dynamic array of structs
I'm trying to return a dynamic array of structs created on the fly from storage data, similar to how we return ViewModel from DB data in MVC. What is the best way of doing it in Solidity?
Tried ...
0
votes
1answer
93 views
Ganache Storage
I am trying to push data to an address array within a struct array. There are no errors, however, when I check the storage of the array in my contract in Ganache, it remains empty. Is there something ...
1
vote
1answer
338 views
Standard work-around for using a Solidity constant array (which is not supported) in pure function?
I have an array in my contract like this and a pure method which uses it like this (simplified exmaple because my real exmaple is many more lines):
uint[16] constant x = [10, 2, 6, 42, 19, 5, 4, 36, ...
0
votes
1answer
35 views
Declare an string array inside a int array in Solidity
I am using truffle and I am trying to pass during the deployment execution script a double array of prices inside my contract's constructor.
ie
In the migration file I have a 2 dimensional array. It's ...
0
votes
0answers
15 views
how to pass fix size array via interface in solidity 0.4.25
I used the following code
interface test{
function testfunction(uint[20] memory P, uint[20] memory Q) external returns(uint,bool,uint);
}
It gives the error Location has to be calldata for external ...
0
votes
0answers
13 views
Arrays not showing up inside structures
I am creating a record of people and have created a structure
struct asset{
uint assetId;
string hash;
address creatorAddress;
uint cost;
uint soldCount;
}
struct person{
uint ...
0
votes
0answers
24 views
How to retrieve all the values form the mapping?
Yield farming. Unaudited food token. Anon founder. Crazy APR%. Of course, if it is "too good to be true" it probably is...
I would like to "don't trust, verify" and verify who is ...
0
votes
1answer
16 views
What is an efficient method to append a dynamic array to a dynamic array?
I'm looking for the most efficient method to append a dynamic array to an existing dynamic array.
I know I could do:
bytes4[] private _array1 = [1,2,3];
bytes4[] private _array2 = [4,5,6];
for( i =...
0
votes
1answer
24 views
Storing multiple values in one key mapping dynamically
I am making a blockchain project using solidity which requires storing various objectIDs corresponding to a personId.
The person can add and remove objects and we cannot hard-code the number of ...
0
votes
1answer
59 views
How to encode arrays for solidity function calls
I want to understand how to encode the following arrays in Solidity.
Dynamic Arrays
For example, the following:
function getDynamicUintArray() public pure returns (uint[] memory) {
uint[] ...
0
votes
1answer
41 views
How can I distribute funds to present stakers efficiently?
I am building a smart contract which keep the token holder's eths for a while and give them rewards when they withdraw. The reward is given from the ethers collected by early withdraw of people. If ...
0
votes
0answers
14 views
Acess position in array vs Access key in mapping (uint IDs) - What's the efficient way?
I have a doubt about efficiency in solidity, when searching in lists of integer values, in mapping vs array.
Array
Imagine that I use the array position as element ID, and the content as the value, as ...
0
votes
0answers
23 views
How store memory array in storage
In this code, I want to send an array of bytes32 and store them in the struct but I get the error which is
TypeError: Type string memory[8] memory is not implicitly convertible to expected type ...
1
vote
1answer
28 views
adding an struct instance into a dynamic array of struct objects
Consider this code.
function getDoctorsList(uint[] memory _docIds) public view returns (doctor[] memory) {
doctor[] memory _doctors;
for(uint i = 0; i < _docIds.length; i++) {
...
2
votes
1answer
27 views
How to access the length of a public array?
Say I have the following contract:
contract A {
uint[1] public MyArray;
constructor() public {
MyArray[0] = 56;
}
}
And I want to know the size of MyArray by looking it up ...
0
votes
1answer
29 views
Is it possible to call a public array from another contract?
Let's say there is a uint[] public MyArray declared in contract_A.
Is there a possiblility to create an interface contract_A that would allow to read the values in MyArray from another contract, or ...
0
votes
1answer
61 views
Deleting an array in storage (>= 0.6.0)
I need to add elements to a resizable myArray and then delete the myArray. I don't know what the length of myArray will be before the loop, and it's not possible to resize a memory array; that leaves ...
0
votes
0answers
12 views
EthersJS returns array double wrapped i.e [ [“0x838383…”, “0x3BC2…”, ] ]
I have a method that is supposed to return an array of deployed contract addresses, it works albeit the value that is returned client side is wrapped with an extra set of brackets and also when ...
1
vote
0answers
28 views
Checking if an item present in an array in Solidity
I want to check whether a specific item is included in an array or not. For example, consider the following array.
uint[] a = [1, 2, 3, 4]
Is there any way that I can get true if I have given 4, and ...
0
votes
1answer
11 views
push to a temporary array within a function
I come from nodejs and this seems odd to me..
can't seem to push anything to a temporary array.
function get_unconfirmed() public view returns (address[] memory, uint[] memory, uint[] memory) {
...
0
votes
2answers
524 views
Solidity & truffle: get elements from strings array
i'm glad to follow this forum, it is very helpful.
I'm using truffle to develop contracts and i just found that dynamic arrays are not yet suported, for that reason i was using string arrays with a ...
0
votes
0answers
27 views
join an array of strings and map, filter, reduce functions in solidity
I want to concatenate an array of strings into a single string using solidity, just like the array join function in javascript and python. Also I want to do map/filter/reduce functions in an array. Is ...
0
votes
1answer
50 views
Can you have an array of mappings? The mappings have an address key and a struct value
Can you have an array of mappings?
I want to have mappings with addresses as keys and structs as the value.
I want to store these addresses in an array and iterate through them.
so far it looks like ...
0
votes
1answer
455 views
How to Serialize/Deserialize array of bytes?
I'm writing a smart contract and inevitably need to have arrays of bytes as input and output of functions. In remix arrays work just fine but with web3j and web3js there are several issues with arrays ...
0
votes
2answers
323 views
Solidity 0.4.26 check if element already exists in array
How do I make it so this only adds an identifier to the dataArray if it doesn't exist already?
pragma experimental ABIEncoderV2;
contract structWithMapping{
struct Data{
string[] user;
...
2
votes
1answer
82 views
How to return values from dynamic array push function?
How to fetch value from dynamic array push function? Below is the code of pushing function, but how will I fetching that array values?
contract PushStruct {
struct MyStruct {
uint field1;
...
1
vote
4answers
911 views
loop to find the highest value in an array
I have made a struct which has an address called playersAddress and a uint called numbers.
I then have a fixed sized array which has in it the playersAddress and the numbers.
When someone calls ...
0
votes
1answer
127 views
Error when trying to view return values
I've written the following contract
contract Catalog{
address[] internal array;
function pushItem(address _el) external{
array.push(_el);
}
function getArray() external ...
2
votes
1answer
300 views
Member “push” not found or not visible after argument-dependent lookup in address[] storage ref
I am failing to use the push method against a dynamic array declared as a state variable. How can I do this?
I declare a dynamic array of addresses as a state variable like below:
contract Sample {
...
8
votes
3answers
6k views
How to fill dynamic in-memory array
For some reason solidity doesn't allow to push values into memory array
Member "push" is not available in bytes32[] memory outside of
storage.
Here is sample contract code:
pragma solidity ^0.4....