Home AMX User Forum NetLinx Studio

Current Array Status

Hi,

Hope someone can help me here, or at least tell me what I am trying to do is not possible. I've scoured the forum, but found nothing to help.
I am using an array to determine the current open/closed status of 6 room partitions:

INTEGER PART_ARRAY[6] //1 or 0 to state open or closed
INTEGER PANEL_ARRAY[5] //1 or 0 to state which panels in use

All I want to do is read the current status so I can set which slave panels are in use. I assumed the following to work fine, but unfortunately does not:

IF (PART_ARRAY[] = {0,1,1,1,1,1})
{
MASTER_ARRAY[] = {1,0,1,1,1}
}

Can anyone offer any solution to stop my hair falling out.
Much appreciated,

Paul

Comments

  • ericmedleyericmedley Posts: 4,177
    I'm not sure what result you're looking for but one thing is you don't need the []s on the arrays in the IF statement. That implies that you intend to eval one particular cell of the array. [3] would mean the value of cell 3. A blank might imply cell zero. (I think???). Cell zero does not exist in Netlix. It does in other languages like C or Java.

    My guess is that if you run 'MSG ON ALL' in terminal you'll see a 'zero index' error when the line runs. Hope that help
    e
  • SmallSmall Posts: 9
    What I'm trying to do really is save code/time. The second array needs to change according to the contents of the first array, such like:

    IF ((!PART_ARRAY[1])&(PART_ARRAY[2])&(PART_ARRAY[3])&(PART_ARRAY[4])&(PART_ARRAY[5])&(PART_ARRAY[6]))
    {
    MASTER_ARRAY[1]=1
    MASTER_ARRAY[2]=0
    MASTER_ARRAY[3]=1
    MASTER_ARRAY[4]=1
    MASTER_ARRAY[5]=1
    }

    I assumed would work fine as:

    IF(PART_ARRAY[]={0,1,1,1,1,1}) MASTER_ARRAY[]={1,0,1,1,1}
    or:
    IF(PART_ARRAY={0,1,1,1,1,1}) MASTER_ARRAY={1,0,1,1,1}

    But unfortunately does not.

    If it can't be done, I'll just have to do it the long way :(
  • JohnMichnrJohnMichnr Posts: 279
    I would have thought the compiler would barf at the curly braces in the middle of the if statement.

    But now that you mention it - I am not sure that I know how to setup that statement with out doing multiple ifs and &&.

    have you tried
    if(PART_ARRAY = (1,1,1,0,0)) no square brackets no curley brakets? (for the reason that Eric mentioned previously)
  • DHawthorneDHawthorne Posts: 4,584
    It needs quotes when done like that, and definitely leave off the empty [], you only need those when you declare it, or if you are referencing a single element. Think of it like a string.

    if(PART_ARRAY = "1,1,1,0,0"), etc.
  • Spire_JeffSpire_Jeff Posts: 1,917
    If this is something that you need to use multiple times in your code, you could just write a function to handle it.

    Here is a quick attempt at a function:
    define_function integer fnCompareMyArray(integer ArrayIn[],integer CompareToThisArray[]){
    	stack_var integer x;
    	if(max_length_array(CompareToThisArray) > max_length_array(ArrayIn))
    		return 0;
    	for(x=max_length_array(CompareToThisArray);x;x--){
    		if(ArrayIn[x] <> CompareToThisArray[x])
    			return 0;
    	}
    	return 1;
    }
    
    button_event[dvTp,202]{
    	push:{
    		local_var integer nMainArray[6];
    		local_var integer nTestArray[6];
    		local_var integer nTestArray2[5];
    		
    		on[nMainArray[1]];
    		off[nMainArray[2]];
    		on[nMainArray[3]];
    		on[nMainArray[4]];
    		off[nMainArray[5]];
    		on[nMainArray[6]];
    		
    		on[nTestArray[1]];
    		off[nTestArray[2]];
    		on[nTestArray[3]];
    		on[nTestArray[4]];
    		off[nTestArray[5]];
    		on[nTestArray[6]];
                        
    		
    		send_string 0,"'Function returned: ',itoa(fnCompareMyArray(nMainArray,nTestArray))";
    
    		
    		off[nTestArray[6]];
    		
    		send_string 0,"'Function returned: ',itoa(fnCompareMyArray(nMainArray,nTestArray))";
    
    
    		on[nTestArray2[1]];
    		off[nTestArray2[2]];
    		on[nTestArray2[3]];
    		on[nTestArray2[4]];
    		off[nTestArray2[5]];
                        
    		
    		send_string 0,"'Function returned: ',itoa(fnCompareMyArray(nMainArray,nTestArray2))";
    
    		
    		off[nTestArray2[4]];
    		
    		send_string 0,"'Function returned: ',itoa(fnCompareMyArray(nMainArray,nTestArray2))";
    	}
    }
    
    

    But, it seems that Dave's code will do what you want :)
    Jeff
  • SmallSmall Posts: 9
    DHawthorne wrote: »
    It needs quotes when done like that, and definitely leave off the empty [], you only need those when you declare it, or if you are referencing a single element. Think of it like a string.

    if(PART_ARRAY = "1,1,1,0,0"), etc.

    Quality, thanks for that. That sorts out the first part, but unfortunately doesn't work when trying to set the second arrays contents, i.e:

    IF (PART_ARRAY = "1,1,1,0,0") // WORKS FINE
    {
    MASTER_ARRAY="0,0,1,0,1" // Does not work. Sees as string.
    MASTER_ARRAY={0,0,1,0,1} // Also, does not work.
    MASTER_ARRAY=(0,0,1,0,1) // Also, does not work.
    }

    Any ideas?
  • HedbergHedberg Posts: 671
    Have you tried using CHAR variable type as opposed to INTEGER type? Depending on what you do, you may experience some compiler warnings about type, but a type_cast should resolve them.

    A CHAR variable is actually an 8-bit integer and you can use CHAR variables as flags and such so long as you pay attention. If you do, then such things as

    MASTER_ARRAY="0,0,1,0,1"

    will work just fine.
  • SmallSmall Posts: 9
    Never thought of that, I'll give it a shot.

    Cheers,

    Paul
  • Spire_JeffSpire_Jeff Posts: 1,917
    I guess I'll make the suggestion. If you are only dealing with 6 zones, you could go bitwise. This would let you do some things like:
    define_constant
    zone1 = $01
    zone2 = $02
    zone3 = $04
    zone4 = $08
    zone5 = $10
    zone6 = $20
    zone7 = $40
    zone8 = $80
    
    define_variable
    integer PART_STATUS
    integer MASTER_STATUS
    .
    .
    .
    
    if(PART_STATUS & (zone1 + zone3 + zone5))
        MASTER_STATUS  = zone 2 + zone5;
    if(PART_STATUS BAND (zone1 + zone3 + zone5)) //same thing using keyword instead of operator. 
        MASTER_STATUS  = zone 2 + zone5;
    
    
    This also allows you to easily see if any zone is on (if(PART_STATUS) send_string 0,"'Atleast one zone is ON'")

    Jeff
Sign In or Register to comment.