Deleting an Address from a Struct

Hello Guys!

I need a bit of assistance. I think I have already overthought it.

Here is my code, think it’s simple enough.

pragma solidity 0.8.0;
pragma abicoder v2;
contract EntityArray {
    
    struct Entity {
        uint data;
        address _address;
    }
    
    Entity[] public entityStructs;
    
    function addEntity(uint data, address _address) public returns (Entity[] memory){
       Entity memory newEntity;
       newEntity._address = _address;
       newEntity.data = data;
       entityStructs.push(newEntity);
       return entityStructs;
    }
    function deleteEntity() public returns (Entity[] memory){
         entityStructs.pop();
        return entityStructs;
    }
}

What I want to do is remove the last element of the Struct Array (this means removing an address) with the deleteEntity function, but it’s not deleting it. What am I missing? Create Entity works just fine.

Any help as always is appreciated.

Thank you!

1 Like

Hello again Riki, hope you are well my friend.

I just copied your code into my Remix IDE & it compiled and ran without error. I was able to add two structs to the array and then remove the last one. Using the Remix button to check the 1 index of entityStructs threw a revert, telling me that deleteEntity() successfully ran the entityStructs.pop() function.

What specific error are you getting? Have you tried backing up your code & refreshing the Remix webpage (if you’re using Remix)?

Cheers.

My specific error is that when I click on the struct function point after the deletion, it’s still showing the address, not just the number. And yes I am using it on Remix

Thank you for your help again Hudson.

1 Like

Hi @Riki

It gets deleted correctly, you are just looking at the wrong value in Remix.
Look at the transaction log in the console to see the data.

1 - Add value to the array:

image

2 - Check the array:

image

3 - Pop

4 - Check the array:

image

Note also the the last ‘check the array’ reverts :slight_smile:

Cheers,
Dani

1 Like

Yeah guys I overtghought it :smiley: Thank you for the explanation again!

1 Like