Shop OBEX P1 Docs P2 Docs Learn Events
Array Access Help — Parallax Forums

Array Access Help

soshimososhimo Posts: 215
edited 2008-12-26 01:51 in Propeller 1
Okay, I'm butting my head up against the wall trying to provide a public function which returns a value based on an index passed in - the index is an index into an array. The code works fine when I use an actual constant as the index to return a value from the array but as soon as I try to use the value passed in I get a very large number, and it's the same regardless of what index I'm using.

This is a public function in an object that I created.

Here is some sample code that actually works:
' ****************************************************************
' * GetInputValue                                                *
' * Get previously stored value                                  *
' ****************************************************************
PUB GetInputValue(index)
  return inputValues[noparse][[/noparse]ABUTTON]




And here is code that does NOT work
' ****************************************************************
' * GetInputValue                                                *
' * Get previously stored value                                  *
' ****************************************************************
PUB GetInputValue(index)
  if( index >= 0 and index < MAXINPUTINDEX )
    return inputValues[noparse][[/noparse]index]
  else
    return 0




Here is how I delcare inputValues:
CON
   MAXINPUTINDEX = 8

VAR
   long inputValues[noparse][[/noparse]8]




I really don't want to have to put a case statement in there to return the values directly and the array access should work with a variable - or maybe I missed something in the manual, but there are other cases where I use a variable. In fact, in the same object causing problems I have an array of indices into another array (because I couldn't figure out how to do multi-dimensional arrays). TIA

Comments

  • soshimososhimo Posts: 215
    edited 2008-12-26 00:20
    Well, I figured out it wasn't the function, it was how I was calling the function. I have a set of CONs that I was trying to use as the index values. I think my syntax was wrong.

    Here is what I have for the CON section in the object:
    CON
      YSTICK        = 0
      XSTICK        = 1
      XAXIS         = 2
      YAXIS         = 3
      ZAXIS         = 4
      ZBUTTON       = 5
      CBUTTON       = 6
      XAXISDELTA    = 7
      YAXISDELTA    = 8
      ZAXISDELTA    = 9
      ROLL     = 10
      PITCH    = 11
      LASTMOVEPITCH = 14
      LASTMOVEROLL  = 15
    
    



    And here is my call:
    pantilt.GetInputValue(pantilt#ZBUTTON)
    
    



    I think the pantilt#ZBUTTON is what is incorrect. What is the proper syntax for referring to an objects constant values?

    Again, TIA.
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-12-26 00:31
    Hello soshimo,

    I found the error with the following debug-strategy:

    CHECK EVERY SINGLE DAMMED DETAIL BY DEBUGOUTPUT


    PUB GetInputValue(index) 
      
      Debug.Str(string("Index="))
      Debug.dec(index)
      Debug.Str(string(" ",13))
      
      
      if( index >= 0 and index < MAXINPUTINDEX )
      'if( index => 0 and index < MAXINPUTINDEX )
        Debug.Str(string("Index OK Index="))
        Debug.dec(index)
        Debug.Str(string(" ",13))
    
        Debug.Str(string("inputValues[noparse][[/noparse]"))
        Debug.dec(index)
        Debug.Str(string("]="))
        Debug.dec(inputValues[noparse][[/noparse]index])
        Debug.Str(string(" ",13))
        return inputValues[noparse][[/noparse]index]
    
      else
        Debug.Str(string("Index out of range Index="))
        Debug.dec(index)
        return 0
    
    



    this code lead me to the bug
    debugoutput

    Index=2
    Index OK Index=-1
    inputValues[noparse][[/noparse]-1]=50
    
    



    right at the beginning parameter index contains the right value
    after the if-condition it's "-1" all the time

    aha there happends something inside the if-condition

    it is the "index >= 0"

    exactly the ">=" is the bug

    the equal-sign BEHIND the greater sign means store result into variable index
    the correct notation for the condition is equal or greater is
    "=>" the equal-sign must be written INFRONT of the greater-sign

    here the complete democode from my debugging

    CON    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
       MAXINPUTINDEX = 8
    
    OBJ
      Debug : "FullDuplexSerial" 'serial connection to PC       
    
    VAR
      long inputValues[noparse][[/noparse] MAXINPUTINDEX]
      long i
      long value
    
      
    PUB Main
      
      Debug.Start(31,30,0,115200) 'start(rxpin, txpin, mode, baudrate) :
      ''       okay '"rx" and "tx" viewed from Propeller-Chip Propeller-Chip-PIN-Tx ----> PC
    
      Debug.Str(string("START",13))
    
      repeat i from 0 to 7
        inputValues[noparse][[/noparse] i] := i * 10
    
      repeat i from 0 to 7
        Debug.Str(string("inputValues[noparse][[/noparse] "))
        Debug.dec(i)
        Debug.Str(string(" ]="))
        Debug.dec(inputValues[noparse][[/noparse] i])
        Debug.Str(string(" ",13,13))
        'waitcnt(ClkFreq  + cnt)
    
      repeat
        repeat i from 0 to 7
          value := GetInputValue(i)
          Debug.Str(string(" ",13))
          waitcnt(ClkFreq + cnt)
    
    
    PUB GetInputValue(index) 
      
      Debug.Str(string("Index="))
      Debug.dec(index)
      Debug.Str(string(" ",13))
      
      
      'if( index >= 0 and index < MAXINPUTINDEX )
      if( index => 0 and index < MAXINPUTINDEX )
        Debug.Str(string("Index OK Index="))
        Debug.dec(index)
        Debug.Str(string(" ",13))
    
        Debug.Str(string("inputValues[noparse][[/noparse] "))
        Debug.dec(index)
        Debug.Str(string(" ]="))
        Debug.dec(inputValues[noparse][[/noparse] index])
        Debug.Str(string(" ",13))
        return inputValues[noparse][[/noparse] index]
    
      else
        Debug.Str(string("Index out of range Index="))
        Debug.dec(index)
        return 0
        
    
    





    debugoutput

    Index=1
    Index OK Index=1
    inputValues[noparse][[/noparse] 1]=10
    
    Index=2
    Index OK Index=2
    inputValues[noparse][[/noparse] 2]=20
    ...
    
    




    best regards

    Stefan

    P.S.: I tested the syntax "objectname#constantname
    it seems to work

    Post Edited (StefanL38) : 12/26/2008 1:01:56 AM GMT
  • soshimososhimo Posts: 215
    edited 2008-12-26 01:51
    Ah, thank you so much Stefan. I had worked around the problem by specifying a placeholder for the return value using the ':' syntax on the function declaration. I couldn't figure out why that fixed it. Thanks for pointing out that gotcha.
Sign In or Register to comment.