1

How do you delete an array at a certain index in solidity? I'm trying to access the index via key and not the index number.

function deleteAtIndex(address _address) {
    delete sellers[_address];
}

The error I'm receiving is TypeError: Type address is not implicitly convertible to expected type uint256. I also know that deleting this will leave a 0 at the index. Is there any way around this without shifting the entire array since I know it's expensive?

2

You're getting that problem because you're trying to search through the indexes of the array using an address which is not valid, you must search using uints. You can however use a pattern to make the index of the address easily retrievable.

Here's a pattern I use to do just this

address[]   public sellers;
mapping (address => uint256) public arrayIndexes;
function addAddress(address _addr) public {
    uint id = sellers.length;
    arrayIndexes[_addr] = id;
    sellers.push(_addr);
}

function removeAddress(address _addr) public {
    uint id = arrayIndexes[_addr];
    delete sellers[id];
}

One thing to note is that this doesn't preserve order in the array, and whatever address you delete will turn into the nu;l address 0x0000000000000000000000000000000000000000

  • I think you have it switched around right? Shouldn't it be arrayIndexes[id] = _addr ? – JorahFriendzone Dec 18 '17 at 17:26
  • 1
    You could do that, however that would require knowing the index of the address, in order to return the address which doesn't make much sense in this case. So you'd want to map the address to the index id so that all you need to know is the address not the index. Arrays must contain elements of the same type, but they are indexed starting at 0 (element 1), 1 (element 2), 2 (element 3). – hextet Dec 19 '17 at 10:06
0

delete a assigns the initial value for the type to a. I.e. for integers it is equivalent to a = 0, but it can also be used on arrays, where it assigns a dynamic array of length zero or a static array of the same length with all elements reset. For structs, it assigns a struct with all members reset.

> I have implemented it, may be helpful to understand by this simple example

**

And if we remove the element using index it will not leave the gap.

**

http://solidity.readthedocs.io/en/v0.4.21/types.html

contract UserRecord {
    constructor() public { owner = msg.sender; }

    address owner;

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    struct User {
        bytes32 userEmail;
        uint index;
    }

    mapping (bytes32 => User) private users;

    bytes32[] private usersRecords;
    event LogNewUser(bytes32 indexed userEmail, uint index);

    function setUseremail(bytes32 _userEmail) public onlyOwner returns(bool success){
        users[_userEmail].userEmail = _userEmail;
        users[_userEmail].index = usersRecords.push(_userEmail) -1;

        emit LogNewUser(
        _userEmail,
        users[_userEmail].index
        );
        return true;
    }

//this will delete the user at particular index and gap will be not there

    function deleteUser(bytes32 _userEmail) public onlyOwner returns(uint index){
        require(!isUser(_userEmail)); 
        uint toDelete = users[_userEmail].index;
        bytes32 lastIndex = usersRecords[usersRecords.length-1];
        usersRecords[toDelete] = lastIndex;
        users[lastIndex].index = toDelete; 
        usersRecords.length--;
        return toDelete;   
    }    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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