Shop OBEX P1 Docs P2 Docs Learn Events
Get array length? — Parallax Forums

Get array length?

eagletalontimeagletalontim Posts: 1,399
edited 2013-11-17 04:48 in Propeller 1
Is there a way to get an array length, maybe by how many array indexes are set? Some values will be 0 including the first and last values.
variable[0] := 0
variable[1] := 54
variable[2] := 15300
variable[3] := 0
variable[4] := 1
variable[5] := 0

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-11-16 21:01
    Not unless you have some value which will be considered invalid. You could use $7FFF (I think that's the largest positive 32-bit number) as an invalid entry. You'd start by filling the array with this value and then you could have a method search the array until a invalid entry is found.

    Wouldn't it be easier to keep track of the index as the array is filled?
  • eagletalontimeagletalontim Posts: 1,399
    edited 2013-11-16 21:08
    Well, this is part of my EEPROM variable saving thing... If the array length is not long enough, the variables did not all get saved or they are not saved at all. If this is the case, I need the variables to then be saved to the EEPROM before it the prop gets turned off. I was hoping to check the array length of "variables" and if it is not long enough, load the variables via a sub. If they are loaded, do nothing.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-11-16 21:21
    Just use some sort of flag variable which gets written to something other than zero once all the data is loaded. When the Prop restarts, check the variable value. If it's zero, you need to load the variables.

    Have you seen this thread? You can backup variables to their original locations in EEPROM so next time RAM is loaded from EEPROM the variable values get restored to the values of the previous session.
  • Mike GMike G Posts: 2,702
    edited 2013-11-17 04:48
    The array length is known because the values are explicitly set in the post #1 example, a constant. If the idea is to remember the last array element added, then use a variable to hold the number of array elements set.

    Here is a demo.
    CON
      _clkmode = xtal1 + pll16x     
      _xinfreq = 5_000_000
    
    CON
      MAX_ARRAY = 10
      
    VAR
      long array[MAX_ARRAY]
      byte arrayIdx
      
    OBJ
      pst           : "Parallax Serial Terminal"
    
    
    
    PUB Main  | i
      pst.Start(115_200)
      pause(1000)
    
      'Initialize array index counter
      arrayIdx := 0
    
      pst.str(string("Array Length Demo", 13, "------------------------", 13))
      array[arrayIdx++] := 0
      array[arrayIdx++] := 54
      array[arrayIdx++] := 15300
      array[arrayIdx++] := 0
      array[arrayIdx++] := 1
      array[arrayIdx++] := 0
    
      pst.str(string("Array Length: "))
      pst.dec(arrayIdx)
      pst.char(13)
      
      repeat i from 0 to arrayIdx - 1
        pst.str(string("array["))
        pst.dec(i)
        pst.str(string("] = "))
        pst.dec(array[i])
        pst.char(13)
       
    
      
    PRI pause(Duration)  
      waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt)
      return
    
Sign In or Register to comment.