Call function by it's number of order- is it possible?

 

What I want to implement is this:

I have an array, let's say

Int Arr[5] ={0,1,1,0,1}

I also have five functions: Func0, Func1, Func2, Func3, Func4

I want to iterrate through the array and only call those functions that have the number corresponding to the index of the array where the value of the array element is 1. For this exapmle those would be functions Func1, Func2 and Func4.

I could make up a string variable that is the same as the function call, but is there any way to then make trader understand that it is meant to be a function call? Any other ideas how to implement this idea?

Thank you! Any insights are greatly appriciated.

 

You could try using the index as a switch, example below. In any case, I don't think you could avoid creating a laundry list.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
int Arr[5]={0,1,1,0,1};
    for(int i=ArraySize(Arr)-1; i>=0; i--){
        if(Arr[i]==true){
            switch(i){
                case 0: Func0(); continue;
                case 1: Func1(); continue;
                case 2: Func2(); continue;
                case 3: Func3(); continue;
                case 4: Func4(); continue;
            }
        }
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Func0(){Print("Zero");}
void Func1(){Print("One");}
void Func2(){Print("Two");}
void Func3(){Print("Three");}
void Func4(){Print("Four");}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
  1. if(Arr[i]==true){

    Arr is an int comparing to a bool is bogus.


  2. Or how I would code it, to hide the details

    int Arr[5]={0,1,1,0,1};
    for(int i=ArraySize(Arr)-1; i>=0; i--){
    if(Arr[i]==1) Func(i);
    }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    void Func(int i){
        switch(i){
        case 0: Func0(); return;
        case 1: Func1(); return;
        case 2: Func2(); return;
        case 3: Func3(); return;
        case 4: Func4(); return;
    }   }
    void Func0(){Print("Zero");}
    void Func1(){Print("One");}
    void Func2(){Print("Two");}
    void Func3(){Print("Three");}
    void Func4(){Print("Four");}
 
WHRoeder:
  1. Arr is an int comparing to a bool is bogus.


  2. Or how I would code it, to hide the details


1: Yeah bad habits, like I always use int as date-time. Like every-time I post it, I'm like "boy is WHRoeder gonna chew me up for this one" but Oh-well.

2: I like the hiding of the details. I believe the less stuff within the start() the better.

?: Is there any language which allows int Function_Ray[5]={Func0(), Func1(), Func2(), Func3(), Func4()}; because

what I really wanted to code is if(Arr[i]==1){ Print( Function_Ray[i] ) ; } Hope u understand what I'm getting at. To me that would avoid the Laundry List.

Reason: