1

I need help!

Kindly bare with me as I am new to Solidity.

I am trying to use the slice array method to remove the first element in my array while also decreasing the array length.

I have a function that successful inserts elements into the array.

According to the Array slices documentation, "array slices are only implemented for calldata arrays", I am not able to fully understand how to use this information in my code, but I keep getting the below error message every time I run the compiler.

The error message seems to be caused by the deleteUser() function:

Warning: This declaration shadows an existing declaration.

Find below my solidity function:

pragma solidity ^0.8.2;

address[] private userIndex;

function insertUser(address userAddress, string memory userEmail, uint userAge) public returns(uint index)
{
    userStructs[userAddress].userEmail = userEmail;
    userStructs[userAddress].userAge   = userAge;
    userIndex.push(userAddress);
    userStructs[userAddress].index     = userIndex.length -1;

 return userIndex.length-1;
}

  function getUser(address userAddress) public view returns(string memory userEmail, uint userAge, uint index)
{
    return( userStructs[userAddress].userEmail, userStructs[userAddress].userAge, userStructs[userAddress].index);
}


function deleteUser(address userAddress, address[] calldata userIndex) public returns (uint256 length)
{
     userIndex[1:];

     return userIndex.length;
}

In Remix, running the getUser("0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2") yeilds:

0:string: userEmail [email protected]
1:uint256: userAge 35
2:uint256: index 2

Kindly help me understand how I can use the array slice to delete the first element in my array in my deleteUser() function

1 Answer 1

2

Since it can only be used with calldata at this point in time, it doesn't seem all that useful for your scenario. Code below works, but I wouldn't want to use it.

It will be good when it can be used with storage & memory variables and doesn't rely on external input.

In the meantime, the usual way to delete an item from an array is to shift all the elements after it down one and then pop the last element. not great on gas, but there you have it.

pragma solidity ^0.8.2;
import "hardhat/console.sol";

contract Test {

    uint[] public userIndex = [0, 2, 4, 6, 8];

    function getUserIndexes() external view {
        for (uint i; i < userIndex.length; ++i){
            console.log(userIndex[i]);
        }
    }

    function deleteUser(uint[] calldata _userIndex) public returns (uint256 length)
    {
        userIndex = _userIndex[1:];
        return _userIndex.length;
    }
}

output : getUserIndexes

console.log:
 0
 2
 4
 6
 8

input : deleteUser

[0,2,4,6,8]

output : getUserIndexes

console.log:
 2
 4
 6
 8
5
  • I appreciate your response. Following your suggestion, I am getting this error message: TypeError: Type uint256[] calldata slice is not implicitly convertible to expected type address[] storage ref this error message points to: userIndex = _userIndex[1:];
    – SirBT
    Jun 16, 2022 at 15:59
  • Actually... I don't mind that the deleteUser() function relies on external input.
    – SirBT
    Jun 16, 2022 at 16:15
  • 1
    yeah, I switched to uint for simplicity in the example and not having to input a bunch of long addresses. you could convert it back. both the uint in the function parameters and the state variable
    – sola24
    Jun 16, 2022 at 16:17
  • Yeah see your point, because after switching it back to address[] Remix wasn't able to capture all addresses, so your approach was much better. Tell me, instead of the address[] how do I use the ` userStructs[userAddress].index`? Looking forward to your response
    – SirBT
    Jun 16, 2022 at 16:58
  • This is absurd Solidity sirs... Please, develope functions to manage arrays ! (or I will end up thinking that you love that we use GAS. So good for your pocket) Sep 9, 2022 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.