2

Here is a contract from https://www.etherchain.org/account/0xc18a451d4ef2a85e7bf8404131c5f994a98bdb15#code

What exactly does idx mean or do?

    function enter() {
    if (msg.value < 500 finney) {
        msg.sender.send(msg.value);
        return;
    }

    // add a new participant to array
    uint idx = participants.length;
    participants.length += 1;
    participants[idx].etherAddress = msg.sender;
    participants[idx].amount = msg.value;

    // collect fees and update contract balance
    if (idx != 0) {
        collectedFees += msg.value / 10;
        balance += msg.value;
    } 
    else {
        //code 
    }
1
  • If the answer helped you, please don't forget to accept it as the correct answer by clicking the tick sign below voting arrows. This applies to all of your questions.
    – niksmac
    Commented Mar 22, 2016 at 2:43

1 Answer 1

4

The line uint idx = participants.length; gets the length of the array and participants.length += 1; this one adds 1 to it. Finally participants[idx] adds a new key to the array and assign new participants to it.

An alternative will be a simple push and this #83 might be the reason why the coder avoid push

2
  • Is idx just the name of the variable?
    – Vesa
    Commented Mar 22, 2016 at 1:38
  • @Vesa, yes :) uint idx = participants.length;
    – niksmac
    Commented Mar 22, 2016 at 1:39

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.