Menu

Table name variable?

Help
J Olson
2009-03-24
2013-05-30
  • J Olson

    J Olson - 2009-03-24

    Can you call a table with a variable name?
    I get errors when I compile.
    "table not defined" etc.
    I want to call a table by setting it elsewhere in my code.
    That way I can load a table on the fly.

    Like:

    Dim table_var as string
    Dim lookup_var as byte

    lookup_var = 0
    table_var = "first_table"

    start:  

       ReadTable table_var, 2, lookup_var

    goto start

    TABLE first_table
    1
    2
    3
    4
    END TABLE

    TABLE second_table
    5
    6
    7
    8
    END TABLE

    TABLE third_table
    9
    10
    11
    12
    END TABLE

    -Zm-

     
    • kent_twt4

      kent_twt4 - 2009-03-24

      Doesn't look so good trying to have a variable equal a string (which is an array of bytes depicting ascii) calling an Org xxxx location in program memory.  Think the best way is to use a bunch of conditional statements, like IF or Select Case, to read the right table based on your code. 

       
    • J Olson

      J Olson - 2009-03-24

      Yea, I know about the var/string not valid.
      This was a quick example to display visually my thoughts.

      I was wondering if there is a way to make the table name a variable.
      Based off your response I assume there is not.

      I currently am using a case stament but it is getting really long.
      If I could just name the table on the fly I could get rid of tone of lines.

      I can simulate in VB no problem, but GC doesn't behave like I would expect.

      Thanks for the quick feedback.

      -J-

       
    • kent_twt4

      kent_twt4 - 2009-03-24

      Actually the var/string is valid I believe, try and complile: MyID = "123xyz"

      Haven't really thought too hard on how a string to variable conversion would work.  Perhaps some manipulation of the source code could work?

      If you have a higher end PIC with lots of ram, another option could use array's instead.  Not really sure how it would play out with your code, it still seems the conditionals would be necessary.

      dim tableOne(4)
      dim tableTwo(4)
      tableOne() = 1, 2, 3, 4
      tableTwo() = 5, 6, 7, 8
      ...
      ...

      Another option might involve using the eeprom address.  At least, reading the eeprom address lends itself well to a variable.

       
    • Nobody/Anonymous

      How about this? It doesn't work either.
      Using a var for the index of the array.

      Dim i as byte
      Dim j as byte
      Dim my_array(10)

      i = 1
      j = 0
      my_array(1) = 1
      my_array(2) = 2
      my_array(3) = 10

          do until j = 10
          j = my_array(i)
                         
          i = i + 1
          loop

       
    • J Olson

      J Olson - 2009-03-24

      Oops, that one works I posted the wrong code snippet.
      It's when I try to make an array() = another array() like:

      Thought I could apply my table logic from above but use the array call instead.

      Dim i as byte
      Dim data_var as byte
      Dim my_array(10)
      Dim another_array(10)

      do until data_var = 255
         data_var = another_array(i)
         temp_array(i) = another_array(i)   
         i = i + 1
      loop
         

      another_array() = 1, 255     'ect

       
    • J Olson

      J Olson - 2009-03-25

      I just got this working.
      I had declared the array at the bottom of my code with all my functions and tables.

      As soon as I moved the arrays (or pre loaded ones) to be at the top with the Dim's it works.

      So my assuption is they must be declared at the start of the code.?.

      You cannot make an array = another array that is null or has no data in it.

      Dim i as byte 
      Dim data_var as byte 
      Dim my_array(10) 
      Dim another_array(10) 

      another_array() = 1, 255 'ect

      do until data_var = 255
      data_var = another_array(i)
      temp_array(i) = another_array(i) 
      i = i + 1
      loop

      -Zm-

       
    • J Olson

      J Olson - 2009-03-25

      The array was the answer.
      I am able to change/load a temp array on the fly.
      Based on some other array's data.

      An example of what I used below.
      It loads RT_array data into the temp_array.
      Simple but solves my problem.

      I use temp_array about 25 times but load it elsewhere in my code.
      This worked like a charm and makes calling the data up very easy
      based on a key press, value etc.

               j = 1
          data = 0

          do until data = 255       
             data = temp_array(j)
             temp_array(j) = RT_array(j)   
             j = j + 1
          loop

      -Zm-
         

       
    • kent_twt4

      kent_twt4 - 2009-03-25

      Glad you got it going the way you wanted.  Lots a times if it works, then that's all that counts for me, and off on some other project.  If you were interested in chopping some more code off (assembler that is) you can save it by:

             j = 1
          data = 0

          do until data = 255      
             data = temp_array(j)
             temp_array(j) = data ;<<==saves some assembler code 
             j = j + 1
          loop

       
    • J Olson

      J Olson - 2009-03-25

      Unfortunatly that won't work for my application as I load temp_array with what's in RT_array, then later I load temp_array with data in another array, on and on etc etc. All based off the user actions.

      I am reading in the temp_array only to get the value of 255 to exit the loading loop. 255 is my "end of this arrays data" flag. So when it sees 255 it stops loading the temp_array and returns.

      This way I can load up the temp_array with some other arrays dataset once an action has happened or the user presses some buttons.

      I was looking for readability and flexibility in the code.
      All I do know is add some more data to an array and add an extra case statment to act on the new entry. Makes it easy to add or delete functionality.

      I have lots of actions I needed to string together in sequences.
      Then play them like a record (LP for you old timers-lol) thru a few subroutines.

      When trying to just put all my arrays in the main code it got long and very complex. It was very hard to follow the flow and debug.
      This solution worked. My main sub is only a page now instead of about 8.

      Also thanks for the quick responses, helped out alot.

      GCB Rocks!

      -Zm-

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.