Home AMX User Forum NetLinx Studio

Variables as Variables

I have several sets of arrays with similar data, Array1, Array2, Array3 and I have a set of identical functions that is triggered by the nSelectedInput Switch. Right now I've had to create 3 cases, one pointing to each different array. Is there a way to condense this so that the Array Selection is a Variable? The selected array will be pulling from other district arrays, so they cannot be combined into one very large array.
SWITCH (nSelectedInput)
	{
	    CASE 1 : 
	    {
		//Do Something with "Array1" (very long)
	    }
	    CASE 2 : 
	    {
		//Do Something with "Array2" (Identical to case 1, except for array name)
	    }
	    CASE 3 : 
	    {
		//Do Something with "Array3" (Identical to case 1/2, except for array name)
	    }
}

would like to do something simpler like:
SWITCH (nSelectedInput)
	{
	    CASE 1 : 
	    {
		SelectedArray = Array 1 //Array1 Selected
	    }
	    CASE 2 : 
	    {
		SelectedArray = Array 2 //Array2 Selected
	    }
	    CASE 3 : 
	    {
		SelectedArray = Array 3 //Array3 Selected
	    }
}

//Now do something with the SelectedArray[x][x][x] (very long)

Comments

  • You could use a multidimensional array.
    INTEGER multiDimensionArray[3][number of elements required]
    

    Now just reference the desired array with your SelectedArray value.
    someVar = multiDimensionArray[SelectedArray][arrayIndex]
    

    Edit: Or are you saying that you can't use a multidimension array in this case? If so then use a function that takes an array as a parameter.
  • ericmedleyericmedley Posts: 4,177
    Another way to do this is put the array in a structure then declare 3 instances of the structure.
    Define_Structure _sMyStruct{
    
    Integer  MyArray[10]
    
    }
    
    define_variable
    
    _sMyStruct MyStruct[3]
    
    // and then in the switch case
    
    Switch(nSelectInput){
        Case 1:
        Case 2:
        Case 3:{
            myStructure[nSelectInput].MyArray[1]=something based on whatever into the first cell of MyArray.
            }
        }
    

    As you can see you can probably even ditch the switch/case since the channel ID will do the selecting for you.

    A multimensional array essentially does the same thing. A structure is nice for organizing lots of different types of data in the same block. Things like mixtures of integers, chars, etc...
Sign In or Register to comment.