Questions tagged [arrays]

Questions regarding the use of Arrays in solidity.

Filter by
Sorted by
Tagged with
1
vote
1answer
32 views

Finding values within an Array/Mapping based off min and max values

I want a user to be able to place a bet on some number, between a range of numbers. So the user wants to place a bet on the numbers 50-500. Then there is a function that sees if the number has been ...
0
votes
1answer
20 views

What if millions of records to be fetched from blockchain?

This is just a hypothetical scenario. Let's suppose someone saved million of information in blockchain with huge gas consumption so far. As for reading data in ethereum smart-contract cost no gas. ...
0
votes
1answer
26 views

The below code gets compiled but when foo is called invalid opcode error is thrown

pragma solidity >=0.5.0 <0.7.0; contract Mycontract{ uint256 a; struct User { string name; } User[] users; function foo() public{ User storage user=users[0]; ...
1
vote
1answer
19 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
21 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 ...
0
votes
1answer
9 views

array.push replacing last value instead of adding to the array

I am attempting to keep a log of when a user does certain things but when a new log event is reached the second log is overwritten instead of adding a new one to the array I am unsure as to why when ...
0
votes
1answer
42 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
1answer
20 views

Adding Struct to Array - Invalid OpCode - Solidity 0.6

I've been wreaking my brain on this for a while, could use some help. Bottomline, the newProposal function throws an VM Exception while processing transaction: invalid opcode error. The puzzling thing ...
0
votes
0answers
11 views

Dynamic two-dimensional array initialisation

I want to initialise a two-dimensional dynamic array. For a one-dimensional array it looks like that: function verify(uint[50] memory input){ uint[] memory input_values = new uint[](input.length); ...
1
vote
0answers
28 views

how to display a struct inside mapping inside struct in solidity on react js frontend

I have a mapping of struct (mapping (address => struct)) inside another struct. I'm having issue in how to display the struct in the front end while using Reactjs.
0
votes
1answer
32 views

Is there an “in” keyword in Solidity similar to Python? [duplicate]

I understand that "in" is a reserved keyword in the Solidity language (for now) but can it be used like the "in" keyword is used in the Python language? That is can it be used to find a specific value ...
0
votes
2answers
61 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; ...
0
votes
1answer
19 views

TypeError: Operator == not compatible with types bool and uint256

I am receiving the error: 'TypeError: Operator == not compatible with types bool and uint256' my code is: if (orderedScreening[0]/4 == orderedScreening[1]/4 == orderedScreening[2]/4 == ...
2
votes
1answer
83 views

How to pass 0 length array as parameter on EtherScan contract editor

suppose one of the function input parameter is bool[] testInput sometimes I want to pass 0 length array like this => [] instead of this => [true,false,true], how does it possible with etherscan.io. ...
0
votes
0answers
17 views

How can I return string array in solidity?

This is my struct: struct Token { address tokenAddress; uint256 minAmount; bool emergencyUnlock; TokenStatus status; string name; uint256 decimal; string thumbnailUrl; } ...
0
votes
1answer
21 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, -...
0
votes
1answer
34 views

Accessing array within struct in a mapping using web3.js

I have created a struct patient as follows, and a mapping of uint->patient. struct patient { uint pId; string name; string phone; address payable owner; uint numRecords; uint[]...
0
votes
1answer
22 views

How to delete an element of array in efficient manner?

pragma solidity ^0.4.24; contract test{ uint[] public array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; function removewithgap(uint index) returns(uint[]) { if (index >= array.length) ...
0
votes
1answer
28 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 ...
0
votes
2answers
44 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); ...
0
votes
0answers
14 views

Arrays copy memory in Solidity

I have this simple code: uint256[] memory result = new uint256[](5); result[0] = 10; uint256[] memory result1 = new uint256[](5); result1 = result; result1[1] = 20; return result; ...
2
votes
1answer
27 views

Why is the first item of an int array type casted to an int?

I was reading a book on solidity, the array was defined as int[5] age= [int(10),20,30,40,50] When data type int was declared at the start then why it is declared again in the array with 10?
0
votes
0answers
31 views

Computing matrix determinant in Solidity

I am trying to write a solidity contract to compute Matrix determinant. I am following the C++ implementation from here: https://www.tutorialspoint.com/cplusplus-program-to-compute-determinant-of-a-...
0
votes
1answer
23 views

Unknown Parameter to Push Values to Dynamic Struct Array

I am trying to use Web3.js to push values from my frontend to a dynamic struct array in my backend. However, I am receiving an error that I need to provide a parameter to my frontend: "Unhandled ...
1
vote
1answer
17 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 ...
0
votes
1answer
21 views

Mapping multiple structs to a struct and call them

I have a question about how I should design my contract to achieve the following: I want to create products identifiers (can be struct or string) something like: "product#123abc". Then I want to map a ...
1
vote
1answer
41 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 ...
0
votes
1answer
27 views

how to do sales receipt

I have a marketplace. I wanted to make a sales receipt for each item sold. The sales receipt will indicate address_receipt,nameOfGood, amountOfGood,Price, sumPrice,address_transaction. What should I ...
0
votes
2answers
29 views

Using a loop to ~ reset a storage array?

How am I supposed to reset an array with previous values to just an empty array? Can't figure it out for the life of me. Using solidity version 4.26, tried... tokenContract[tokenAddress].someArray = ...
0
votes
2answers
53 views

Costs of storing an empty dynamic array of length 2**256-1 vs length 4

If the array is completely empty, is it more expensive to store uint[] of length 2**256-1 vs of length 4?
1
vote
2answers
30 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
2answers
148 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 ...
0
votes
1answer
56 views

How to properly initialize nested storage array/struct?

contract Contract { struct A { int a; } struct B { A[] as; } mapping(address => B[]) bs; function addB(B memory b) { bs[msg.sender].push(...???...); } } I have nested hell of arrays and ...
0
votes
0answers
33 views

How to check if key exists in array of bytes? [duplicate]

I have an array of bytes10 inside a mapping, like this: mapping(uint => bytes10[]) public Players; I want to check if a certain index exists in the bytes10 array. I tried to check it with ....
0
votes
4answers
101 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 ...
3
votes
1answer
426 views

How can I slice bytes, strings, and arrays in Solidity?

Is it possible to slice variables in Solidity?
1
vote
1answer
24 views

arrays and structs for function arguments

A lot of contracts and libraries with functions that use arrays and structs for arguments. Structs can themselves include arrays, structs and mappings. For example: function doSomething(MyStruct ...
0
votes
1answer
22 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
36 views

Passing of a struct array from called contract to calling contract

I am currently developing a dAPP and I am splitting it in 2 distinct contracts. I will be calling functions from contract A to contract B. In contract B I have an array of struct which I can ...
0
votes
1answer
41 views

Passing array of struct as parameter: Only libraries are allowed to use the mapping type in public or external functions

I am trying to push multiple items to the array of structs in the contract. Couldn't find a clear answer if it is possible to pass an array of struct as a parameter. Apparently I can't but I get a ...
0
votes
1answer
80 views

Display matrix in React

I am trying to return a matrix in React that I get from a contract function. This is the function that resonate the matrix: int[][] public publicData; function handleAddData(int[] memory ...
0
votes
0answers
118 views

Web3- Array returned from function is always empty

I have contract in solidity and I like to return array or array by index. I'm using experimental feature on, and in remix in solidity I don't have issue with returing the array, but when I use web3 ...
2
votes
0answers
39 views

Performing a C-like pointer increment in Solidity

I have a memory or calldata struct with an attribute of type byte[]. I want to have a memory variable with that data, except for the first n Bytes. In C, you'd just write typename variablename = ...
0
votes
1answer
29 views

Trying to get array size but it reverts

I wanted to get array length by getter function, but it keeps reverts. But I have no idea why it's reverting. Here is my code. struct ItemAddress { uint8 sendNatCd; uint8 rcvNatCd; uint16 ...
1
vote
2answers
231 views

Array Costs in Solidity

Why is storing in an array the first time in solidity costs higher than the other times? The other times it has a constant cost which is less than the first time.
1
vote
1answer
51 views

Add Number to Item in all Struct Members

I have a struct where I want to increment an item across all struct members. For example, say there is a struct 'Contract', and new customers get their own bytes32 ID, which points to their own struct:...
0
votes
1answer
29 views

Query each struct of a Mapping vs looping getter function for front-end?

I have a mapping of structs and a counter variable to keep track of how many structs are stored in the mapping. For front-end development would you recommend to query each struct using the auto-...
0
votes
0answers
15 views

loop into an array and extract infos for specific address

I'm new in solidity . I'm trying to make a smartContract as bellow pragma solidity ^0.4.18; pragma experimental ABIEncoderV2; contract AnalyseContract { //struct => new type of data for this ...
0
votes
0answers
21 views

What is an efficient way for converting 'bytes4' into 'bytes' or 'byte[4]'?

I am currently converting bytes4 into bytes like this: function convert(bytes4 x) public { bytes memory y = abi.encodeWithSelector(x); ... } Is there a more efficient way? Also, is there an ...
0
votes
1answer
108 views

Check if Struct already exists in array of structs

I am new to solidity. I have a struct and an array of structs, like this: struct Adopter{ uint age; uint gender; // 0 woman, 1 man // maybe add an id field } Adopter[] adopters; So, a ...

1
2 3 4 5
8