0

There is a contract where we have artists and anybody can buy an artist, then he becomes the owner of this artist. Also, I have owners array and bulkSend function that sends ETH to all addresses of the owners array.

pragma solidity >=0.4.22 <0.7.0;

contract Cinema {

    struct Artist {
        string name;
        uint price;
        address owner;
    }

    Artist public artist; 
    mapping (uint => Artist) public artists;
    address payable private manager;
    address payable[] public owners;
    uint numArtists;

    constructor() public {
        manager = msg.sender;
    }

    function bulkSend(uint256 amount) public payable {
        uint256 share = amount / owners.length;
        uint256 length = owners.length;
        uint256 currentSum = 0;
        for (uint256 i = 0; i < length; i++) {
            require(share > 0);
            currentSum += share;
            require(currentSum <= msg.value);
            owners[i].transfer(share);
        }
    }

    function addArtist(string memory name, uint price, address payable owner) public returns(uint artistID) {
        artistID = numArtists++; // artistID is return variable
        artists[artistID] = Artist(name, price, owner);
        owners.push(owner);
    }

    function contribute(uint artistID) public payable {
        Artist storage c = artists[artistID];
        require(msg.value == c.price, "Wrong amount");
        manager.transfer(msg.value / 10); // Send 10% to the manager
        bulkSend(msg.value * 9 / 10); // Send 90% to owners
        c.owner = msg.sender; // Set contributor as owner of the Artist
        delete owners[c.index];
        owners.push(msg.sender); // Add contributor to the owners array
    }
}

The question is can I delete an element in the array and somehow past a new element at this position (.push doesn't allow to do this)? It's important to keep the order of the elements.

  • Please rephrase the words "past a new element", this is not proper English. Do you mean insert the new element at that position? That's not a problem, but it violates the purpose that you have stated further down the same sentence - "important to keep the order of the elements". Your question is thus extremely unclear IMO. – goodvibration May 9 at 13:25
1

If you are trying to remove and element at a specific index then add another element to the same index, just simply do this

delete owners[i];

this will remove the element.

then to add another element at a specific index you can do this.

owners[i] = address;

make sure to replace address with a real address like the one which has been passed to the function by the user.

| improve this answer | |
  • It's working! Looks like I don't need the delete_element function at all. Thank you. – Vladimir B. May 9 at 16:13
  • you're welcome :) – Masoud jt May 9 at 16:16

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.