Questions tagged [arrays]

Questions regarding the use of Arrays in solidity.

Filter by
Sorted by
Tagged with
1
vote
2answers
370 views

Display Array of struct within array of struct in react

I'm trying to create 2d array of different types, one of the users have suggested to use struct within a struct method -> 2d array of different types? Now I run into problem how to display it in my ...
1
vote
2answers
2k views

Should I be shifting array elements as I remove them?

I'm storing pending operations in a data structure similar to the multisig. wallet contract, i.e. struct PendingState { uint yetNeeded; uint ownersDone; uint index; } and ...
1
vote
2answers
507 views

Should I use the 'external' modifier to return a large array?

If I need to access a large array from my contract... say for my front end. Should I be using external to make it cheaper for myself? A comment by William Entriken on this question is what got me ...
1
vote
2answers
2k views

Solidity: How could I apply delete to complete storage ref[] with one call?

Definition: mapping(address => data) clusterContract; struct data { //defined inside a Library. mapping(address => Library.Struct[]) my_status; } data list; ...
1
vote
1answer
130 views

Array and mapping are not pointing to the same struct

I am building a DApp game and I am experiencing a problem. Let me describe it. I have a struct: struct NPC { uint id; string name; uint level; } Then I have an array that stores all the ...
1
vote
2answers
501 views

Invalid opcode exception when trying to resize a storage array

I wrote this function: function removeClaim(uint256 _claimId) public returns (bool success) { require(msg.sender == owner || msg.sender == claims[_claimId].issuer); // Emit event and store ...
1
vote
4answers
641 views

Mutating slot 0 of a storage array in Solidity assembly

Have been working on a function allow users to edit specific types of state using Solidity assembly, and ran into some unexpected behavior. According to the documentation, for a storage array arr ...
1
vote
2answers
327 views

Returning dynamic arrays: out of gas

I'm trying to return a dynamic array of structures from a Solidity function to a client. Since I need to build the result, I'm declaring a storage variable and add elements to it: function ...
1
vote
1answer
3k views

Hidden problems in "set array to empty array to clear it" approach

I needed a way to clear a storage array in my contract. I've seen this answer, but I tried to think of different approach (My arrays are most probably will contain enormous amount of elements) and ...
1
vote
2answers
2k views

Deleting an array element. Need to resize to return correct length? [duplicate]

I want to get rid of the last item of a storage array, and have array.length return the proper value. Currently I'm using delete to zero it out. However, when I then call array.length, it returns the ...
1
vote
1answer
2k views

Solidity "call" function with array as input

I have a function on contract A for examples : uint256[] values; function set(uint256[] _arrayToSet){ values = _arrayToSet; } It works perfectly when called directly, but I would like to call it ...
1
vote
1answer
756 views

Invalid implicit conversion from address[2] memory to address[] memory requested

This is so frustrating and I don't know why these errors don't give more information. I compiled and deployed a contract in remix using the left panel and everything is working fine. I then proceeded ...
1
vote
3answers
73 views

Returning an array of structs

Here is my code example: pragma solidity ^0.5.16; pragma experimental ABIEncoderV2; contract Operations { Operation[] public operations; struct Operation { uint startOperation; ...
1
vote
1answer
386 views

Drizzle - accessing array items from a getter

In my contract I have a getter function that returns a full array of addresses: function getDeployedCampaigns() public view returns(address[] memory) { return deployedCampaigns; } Using drizzle ...
1
vote
2answers
1k views

Efficient way of checking and inserting array address that is unqiue

I have a following requirement that need to be cater in dapp. Insert array of address The array need to be unique. If one of the array address already exists, reject and revert entire operation. ...
1
vote
1answer
86 views

Assigning Array Parameters

Attempting to assign array parameters globally bytes32[] params; (params[0], params[1]) = ("p1", "p2"); results in ParserError: Function, variable, struct or modifier declaration expected. When ...
1
vote
1answer
3k views

mapping to a mapping? or mapping to a struct

Is it possible to do something similar to the below? Currently when compiling it will throw an error saying that "Name has to refer to a struct, enum or contract". I also have a "working?" solution ...
1
vote
1answer
94 views

Fixed-size array issue

Maybe this question it seems to be stupid but is it possible to change elements of fixed-size array like bytes32. For example: bytes32 _b32 = sha3(1); _b32[0] = 100; // => Error
1
vote
2answers
488 views

Returning static array inaccessible dynamic type

In the first contract I have: function getArray() external view returns(uint[] memory result) { uint counter = 0; result = new uint[](3); for (uint i = 0; i <= 3; i++) { result[...
1
vote
2answers
222 views

Parsing contract method output as an array of fields

I have a constant method in a Smart Contract that returns this array: (uint256 _field1, uint256 _field2, uint256 _field3, address _field4, string _field5) When invoking it I got this hex result: ...
1
vote
2answers
25 views

How to initialize an array inside the function and push items into it?

How to initialize an array inside the function and push items into it? I'm not gonna initialize outside the function. Here's my code: function tokenOfOwner(address owner) public virtual returns (...
1
vote
1answer
41 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++) { ...
1
vote
1answer
42 views

Array of mapping

I would like an array of mappings, is this possible? mapping (address >= value) sentValue; sentValue[] public sentValues; I want to record amounts sent by address, and I want each send event to ...
1
vote
1answer
38 views

How does array.length-- work?

I'm having trouble understanding why people call array.length-- after deleting an element in an array (as seen in this answer and many others). Why is there need for doing so? When is the length of ...
1
vote
2answers
2k 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; ...
1
vote
1answer
167 views

How do you create an array in vyper?

How do you create an array in vyper? I want to create the array below but vyper is confusion. Outcomes = [5, 500, 5000, 50000, 500000000, -1, -100, -1000, -10000, -100000, -500, -5000, -50000, -...
1
vote
1answer
47 views

Can I see an example that shows the use of .arrays and push(unint x) method in solidity 0.6?

I'm having problem to understand why the method push() does't work on Remix using Solidity 0.6. I have read the documentation about arrays fixed size and dynamic size doc reference for example in ...
1
vote
1answer
831 views

Weighted Random Number Array generation in solidity

I have an array like this in the array, uint256[] public numberArr= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Each number in an array have their own weight, uint256[] public weightNumberArr = [1, 3, 9, 7, 15,...
1
vote
2answers
935 views

Calling an address array in web3 returns an error that the method needs arguments

I'm declaring a public address array in my contract, but when I call it, I get an error saying it's expecting an argument. Can someone shine a light on what is happening here? Contract.sol uint ...
1
vote
1answer
55 views

Create Contract Solidity 0.5

My code works with compiler 0.4.25 but not with the latest version. I want to keep track of every "Contract 2" that "Contract 1" create. Contract 1 uint public countContract; address[] public ...
1
vote
3answers
2k views

how do I loop through a mapping of address [duplicate]

I have a mapping of list of struct (mapping (address => struct[])) I'm having a trouble in how to display all them together in the front end (i'm using React js). Thnx.
1
vote
1answer
551 views

Solidity : Error when pushing an address into a dynamic address type array

I'm trying to insert an address into an address type dynamic array. It works well when I do the operation inside a function. There are no errors and the elements are properly pushed into the array. ...
1
vote
3answers
3k 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
132 views

Why is delete on arrays implemented in an energy and gas wasting way?

If I delete the whole array (static/dynamic), then all elements of an array are set to default value. So, it means that if an array has a million elements, then I have to pay gas for setting up of ...
1
vote
1answer
897 views

Changing dynamic array index content

I'm trying to to make a contract which lets the user choose any number he likes to populate an array of integers used in a mapping. Then he can choose any index of this array to increase its value by ...
1
vote
1answer
1k views

How to map Array of Structs in Solidity [duplicate]

Need this architecture { address1 => [struct1, struct2], address2 => [struct1, struct2, struct3,], address3 => [struct1, struct2, struct3, struct4, struct5], ......
1
vote
1answer
235 views

Compare memory pointers in Solidity

Is there a way to compare pointers in Solidity? I have two local variables: uint256[] memory a = new uint256[](256); uint256[] memory b = a; If I try a == b I get this compiler error: TypeError: ...
1
vote
1answer
878 views

How to access array within a struct within an array?

I have a public array of structs. Each struct has a fixed-length array inside of it. How do I go about accessing a specific struct's array member via web3? The contract is compiling, but when I use ...
1
vote
1answer
425 views

double mapping to an array only allows one item?

I have the following: mapping (address => mapping (address => uint[])) public allowed; function approve (address _to, uint256 tokenId) external { allowed [msg.sender][_to].push(tokenId); } ...
1
vote
1answer
101 views

How to download array of strings

EOS requires token holders to generate public keys and register them, by using their "register" function in their smart contract. Here's the function (copied from Etherscan): mapping (address => ...
1
vote
1answer
260 views

An underflow question

c: [ 11579208, 92373161954235, 70985008687907, 85326998466564, 5640394575840, 7913129639935 ] When I "underflow" a unchecked balance of coins, I get the array of ...
1
vote
1answer
731 views

web3.js Contract returning null instead of an array

Simple contract: contract ex { address[] users; function addUser() public { users.push(msg.sender); } function returnUsers() constant returns (address[]) { return users; ...
1
vote
1answer
2k views

How to pass public variable in Remix online compiler

Lets say I have this contract pragma solidity ^0.4.11; contract TestArray { uint[] public original; uint[] public newOriginal; function TestArray(){ original.push(2); ...
1
vote
1answer
13 views

Access arrays from public struct mapping?

I have this struct: struct Amendment { string text; uint24 textColor; bool isRadialGradient; uint8[2] radialGradientCenterPct; uint16 linearGradientAngleDeg; uint24[2][11] ...
1
vote
1answer
22 views

Payout All Items in Mapping

I want to payout multiple addresses in one function call but I am trying to avoid using an array if possible. struct Receiver { address receiver; bool paidOut; bool isWinner; } mapping(...
1
vote
1answer
8 views

What happens if dynamic array index is out of range?

What happens in a function where user can supply an uint256 which reads the data from a dynamic array... What happens if index is out of range? Does transaction revert automatically? Or do i have to ...
1
vote
1answer
48 views

How to initialize a dynamic memory array with a single value in one line of code?

How can I create and initialize the single-value dynamic memory array.. inside the function call acceptsArray(...)? Possible to create it and initialize in one line? function acceptsArray(uint[] ...
1
vote
1answer
35 views

Weird behavior when dealing with uint32 memory array in Yul

There's something I can't understand in the way Solidity handles dynamic-size memory arrays. For instance, let us consider the following contract: pragma solidity ^0.8.0; ...
1
vote
1answer
15 views

Why can't I assign a value to an array from constructor upon deloyment

pragma solidity 0.8.10; contract test{ address[] wallet; constructor() public { wallet.push(0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8); } This compiles but gives a deployment error (about ...
1
vote
1answer
44 views

How do I initialise a dynamic array in a solidity struct for every newly created struct?

contract Foo { struct Bar { address owner; uint[] x; } mapping( uint => Bar) bars; function foobar(uint index) public { Bar memory b = bars[index]; ...

1 2 3
4
5
10