Shop OBEX P1 Docs P2 Docs Learn Events
Possible to Alias an Array? — Parallax Forums

Possible to Alias an Array?

SRLMSRLM Posts: 5,045
edited 2008-08-07 02:40 in BASIC Stamp
Hi. I was wondering if it was possible to alias an array? The first element is no problem, but when a reference (#) is added, such as

numberTwo·VAR numberArray(2)

The compiler displays a message about expectations:

Expected ':' or end of line


Also, using HIGHBYTE or BYTE0 variations does not work. Is there in solution to this?


·

Comments

  • ZootZoot Posts: 2,227
    edited 2008-08-06 21:50
    If you want to alias to an array, you actually work in reverse -- you alias the array itself to the first variable of the referenced elements, i.e.


    buffer0 VAR Word ' first element of implied array
    buffer VAR buffer0 ' implied start of array
    buffer1 VAR Word ' second
    buffer2 VAR Word ' third


    'additional aliases to second element
    someNum VAR buffer1

    Now, to treat it as an array, just use the array element syntax ON THE FIRST variable of the list...subsequent defined vars of the same size will fall into line automatically:

    buffer(0) = 1 ' same as buffer0 = 1, or buffer = 1
    buffer(1) = 2 ' same as buffer1 = 2
    buffer(2) = 3 ' same as buffer2 = 3

    someNum = "XX" ' same as buffer1 = "XX" or buffer(1) = "XX"

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-08-06 21:52
    There is another way to accomplish this…If you declare your variables and then refer to the first variable with an index you will have a virtual array. Take the code below as an example. When this code is run the all three variables will be printed even though we only used the first variable name, indexed as a virtual array. The only thing you have to be careful of is how you list your variables because PBASIC allocates them in a certain order by size. Words are all allocated first, followed by Bytes, Nibs and then Bits. I hope this helps. Take care.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    test    VAR   Byte
    work    VAR   Byte
    help    VAR   Byte
    
     
    test = 2
    work = 10
    help = 89
     
    DEBUG DEC test(0), CR
    DEBUG DEC test(1), CR
    DEBUG DEC test(2), CR
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • SRLMSRLM Posts: 5,045
    edited 2008-08-07 02:40
    Thanks for the tips. This should work for my application.
Sign In or Register to comment.