Home AMX User Forum NetLinx Studio

Array index problem

I am trying to update an array by referencing the array index via a var such as ZonePower[var]="1" the code complies but on run time generates an error "index too large". The var is updated from a incoming hex string denoting the zone number and ranges from $01 to $06.

If i alter the code to ZonePower[1]="1" it runs fine but if i go back to ZonePower[var]="1" it fails, even though in the debug window var holds 1 as its value.

i have tried assigning the "var" via different string manipulation types atoi,type cast etc but still seem to be at a loss.

Any pointers gratefully received.

Comments

  • jjamesjjames Posts: 2,908
    Have you tried HEXTOI?
  • ericmedleyericmedley Posts: 4,177
    jjames wrote: »
    Have you tried HEXTOI?

    To further ellaborate on jjames's comment...

    It's a common error. You'll grab a zone value from a text string. Say something along the lines of

    "input3output4"

    and you then do the

    nZone=remove_string bla bla bla where nZone is an integer. to get the value 4. But you forget to do an ATOI so the value entered into nZone is the hex value for the charactor '4' which is Hex 34.
  • mpullinmpullin Posts: 949
    ericmedley wrote: »
    But you forget to do an ATOI so the value entered into nZone is the hex value for the charactor '4' which is Hex 34.
    This is a logical diagnosis, I make this mistake occasionally, but in this case the original poster specifically says he tried ATOI.

    I would check on the size of the ZonePower array.
    Also, in your debugger, where it says var is 1... drag a little to the right. Does it say Decimal or ASCII?
    Finally I would throw a SEND_STRING 0, "'var=',var" right before the offending line to make absolutely sure which cell of the array you're trying to access.
  • a_riot42a_riot42 Posts: 1,624
    xrmichael wrote: »
    I am trying to update an array by referencing the array index via a var such as ZonePower[var]="1" the code complies but on run time generates an error "index too large". The var is updated from a incoming hex string denoting the zone number and ranges from $01 to $06.

    If i alter the code to ZonePower[1]="1" it runs fine but if i go back to ZonePower[var]="1" it fails, even though in the debug window var holds 1 as its value.

    i have tried assigning the "var" via different string manipulation types atoi,type cast etc but still seem to be at a loss.

    Any pointers gratefully received.

    Post the code.
  • Code

    It shows as DEC or HEX in the debug the incoming data is in hex format, but when referencing an array index i presume you could use hex or dec. Also the inoming data has no termination byte, it has a start but i frequently find the start byte appears again as valid data in the string hence the if line below.
    IF (FIND_STRING(mzcrecbuf,"$55",1))
    		{
    	    ss = FIND_STRING(mzcrecbuf, "$55", 1) //find sync byte
    	    sl = mzcrecbuf[(ss+1)] //get string length
    	    
    		if (mzcrecbuf[(ss+sl+1)]="$55") //check for full string - strings not terminated
    		{
    		replya = mid_string(mzcrecbuf,ss,(sl+1))
    		remove_string(mzcrecbuf,replya,1)
    		zone = mid_string(replya,4,1) // tried type_cast here
    		SEND_STRING 0,"'zone',zone"
    		ZonePower[zone]="1" //tried atoi itoa hextoi
    		}
    		else
    		{
    		//REMOVE_STRING(MZCRECBUF,"$55",1) // look at this later
    		}
    		}
    
  • viningvining Posts: 4,368
    xrmichael wrote:
    but when referencing an array index i presume you could use hex or dec.
    Nope, needs to be a decimal (integer) value, you'd need to use HEXTOI(HexValue) is you wish to use a hex value to reference an array position and then that converted value would have to be within the bounds of the defined array. Not zero or above define length of the array.
  • Joe HebertJoe Hebert Posts: 2,159
    xrmichael wrote:
    when referencing an array index i presume you could use hex or dec
    vining wrote:
    Nope, needs to be a decimal (integer) value, you'd need to use HEXTOI(HexValue) is you wish to use a hex value to reference an array position
    That?s not true. You can do all sorts of perfectly legal goofy things if you want. Consider the following:
    DEFINE_DEVICE
    
    dvTP = 10001:1:0
    
    DEFINE_VARIABLE
    
    INTEGER x
    INTEGER nTest[50]
    
    DEFINE_START
    
    //fill in with numbers 2-100
    FOR (x=1; x<=50; x++) {
       
       nTest[x] = x*2
    
    }
    
    DEFINE_EVENT
    
    BUTTON_EVENT[dvTP,1] {
    
       PUSH: {
    
          SEND_STRING 0,"'Array Position $10 = ',ITOA([b]nTest[$10][/b])" 		        //element 16
          SEND_STRING 0,"'Array Position ''0'' = ',ITOA([b]nTest[TYPE_CAST('0')][/b])"	//element 48
          
       }
       
    }
    

    And the output when button 1 is pushed:
    Line      1 :: Array Position $10 = 32 - 12:38:47
    Line      2 :: Array Position '0' = 96 - 12:38:47
    
    vining wrote:
    would have to be within the bounds of the defined array. Not zero or above define length of the array.
    That?s absolutely true.
  • viningvining Posts: 4,368
    Yep, your right but it would be goofy to do something like this:
    SEND_STRING 0,"'Array Position ''32'' = ',ITOA(nTest[TYPE_CAST(' ')])"	//element 32
    or
    SEND_STRING 0,"'Array Position ''63'' = ',ITOA(nTest[TYPE_CAST('?')])"	//element 63
    
    But you can. I know if I came across this in a block of code I'd be scratching my head for awhile. I keep forgetting that real programmers do think differently and can look at letters and numbers and automatically see their equivelent in another data type. I still have a hard time remembering that in the end they all are binary 1's & 0's. Some can read binary like I read Ascii, heck some programmers probably read binary better than I read ascii.
  • Array

    I got it working with the following line, ZonePower[zone] = mid_string(replya,6,1). A tweak to array declaration must have been the problem.

    On to the next issue, the code runs but slow i am missing data my buffer is always full so data is getting pushed out before i have acted on it,

    The unit does not send a Termination Character just a start and also a length, so i figured these lines used in a while loop would on 1st pass remove any incomplete strings and then keep the buffer in check

    if(mzcrecbuf[(ss+sl+1)]="$55") //ss marks the 1st 55 sl contains the length
    {
    replya = mid_string(mzcrecbuf,ss,(sl+1))
    remove_string(mzcrecbuf,replya,1)
    }
    else
    {
    send_string 0,"'incomplete string'" //
    }

    I am getting the 'incomplete string" in my debug 2 times a second so something is wrong.

    Am going about this the correct way, what further complicates it is that a $55 can appear mid string as valid data.

    Thanks. for all the pointers they are invaluable information on the long road to becoming a real programmer.
Sign In or Register to comment.