I am developing a test scenario in Remix (Solidity). I have seen a few sample codes, but am still unclear how this is done. I have attached a sample code (not complete) that would help clarify this.

I have two questions for the scenario below:

  1. How to I create a loop for the Map to print the current list of registered participants. I am using events to do this. But I need a loop to go over the registered participants.
  2. When I delete participant. How can I treat the array of registered keys. participantsKey


pragma solidity ^0.4.11;

contract RaceParticipants {

    struct Participants {

        bytes32 id;
        bytes32 name;
        uint age;
        bool isValue;


    mapping (bytes32 => Participants)  participantsMap;

    bytes32[] participantsKey;

    event LogParticipants (bytes32 id, bytes32 name, uint age);

    function ListRegisteredParticipants () {

        for (.....) {

            LogParticipants (id, name, age);



    function DeleteRegisteredParticipants (bytes32 _id) 
        returns (bool flag) {

        if (participantsMap[_id].isValue) {

            delete participantsMap[_id];

            return true;


        return false;


    function RegisterParticipants (bytes32 _id, bytes32 _name, uint _age) returns (bool flag) {

        if (!participantsMap[_id].isValue) {

            participantsMap[_id].id = _id;
            participantsMap[_id].name = _name;
            participantsMap[_id].age = _age;
            participantsMap[_id].isValue = true;


            return true;


        return false;



1 Answer 1


Some new thinking needed here. Welcome to Ethereum. ;-)

You can't iterate the keys in a mapping. In essence, all keys exist.

Also, it's an anti-pattern to iterate over an unbounded list. At some stage, it will run out of gas due to the block gasLimit.

Therefore, you want to proceed with little functions that cost roughly the same in gas at any scale. That usually implies pushing the iterative processes to clients, and building "one pass" logic in the contract.

Take care to ensure the contract state is "complete" at all times to avoid race conditions and other weirdness.

Have a look here for Mapped Struct with Index that is pretty close to what you want: Are there well-solved and simple storage patterns for Solidity?

Hope it helps.

  • Thanks @RobHitchens. The link you provided takes me to a google hangout page and it gets redirected to Andrew's Hangout session- with a request to join the call. Wondering if the link is correct?
    – userDSSR
    Commented Sep 24, 2017 at 18:25

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.