Shop OBEX P1 Docs P2 Docs Learn Events
Creating aliases to array sections — Parallax Forums

Creating aliases to array sections

southernbobsouthernbob Posts: 34
edited 2005-06-25 18:18 in BASIC Stamp
I am running out of variable space for my program. I am trying to
come up with a scheme for re-using· some variables but give
them descriptive names for each section of code.
This is what I am trying to do:
[noparse][[/noparse]code]
temp······ var· byte(4)··· 'create a 4 byte array
···· 'comments on what alias is used
···· 'temp(0)···· alias: addr,msg_addr(LN),msg_length(HN)
···· 'temp(1)···· alias:iobyte,msg_number(LN)
···· 'temp(2)···· alias:led_num
···· 'temp(3)···· alias: something

'now I am trying to create aliases to various parts of the array

addr······· var··· temp(0)
····· ' ****·· get complier error "expected '.' or end-of-line" ·********

'tried this

addr······ ··· var······ temp.byte0··· 'no error on this
iobyte······ · var······ temp.byte1···· 'no error on this
LED_num· var····· ·temp.byte2··· 'get error: "expected a variable modifier" on the· BYTE2

I am trying to use this method so that in parts of the program, I can do
something like this and reuse variables;
· for index = 0 to 3
····· temp(index) = hr_min.nib(index) + $30
· next
[noparse][[/noparse]/code]

Is there any· way of doing this?

This from the HELP manual;
It's fair game for aliases and modifiers, both in VAR directives and in instructions.

Thanks for any help,
Bob

PS: I am using version 2.2 of the editor and pbasic 2.5

Post Edited (southernbob) : 6/24/2005 9:13:55 PM GMT

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-24 23:08
    You cannot alias array elements ... but the good news is that PBASIC treats all RAM like an array, so what you can do is this:

    temp···· VAR··· Byte······· ' same as temp(0)
    temp1··· VAR··· Byte······· ' same as temp(1)
    temp2··· VAR··· Byte······· ' same as temp(2)
    temp3··· VAR··· Byte······· ' same as temp(3)

    With the "array" declared manually as above you can alias its elements.· The only caveat is that you must define and keep your elements in order.

    As an example of where this implicit array can be useful, this subroutine will stuff the entire contents of RAM into the Scratchpad (for those Stamps that have it):

    Push_RAM:
    · PUT 0, B0
    · FOR B0 = 1 TO 25
    ··· PUT B0, B0(B0)
    · NEXT
    · RETURN

    To "pop" the RAM:

    Pop_RAM:
    · FOR B0 = 1 TO 25
    ··· GET B0, B0(B0)
    · NEXT
    · GET 0, B0
    · RETURN

    Note: Using this code is the only time I think using internal variable names is appropriate.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 6/24/2005 11:13:23 PM GMT
  • southernbobsouthernbob Posts: 34
    edited 2005-06-25 18:18
    Jon,
    Do you mean something like this;
    [noparse][[/noparse]code]
    '{$STAMP BS2}
    '{$PBASIC 2.5}
    temp0·· VAR· Byte
    temp1·· VAR· Byte
    temp2·· VAR· Byte
    temp3·· VAR· Byte
    temp4·· VAR· Byte
    temp5·· VAR· Byte
    index·· VAR· Byte

    bob0··· VAR···· temp0
    bob1··· VAR···· temp1
    bob2··· VAR···· temp2
    bob3··· VAR···· temp3
    bob4··· VAR···· temp4

    bob0· = $11
    bob1· = $22
    bob2· = $33
    bob3· = $44
    bob4· = $55
    FOR index = 0 TO 4······················ 'debug shows 11 to 55
    ·· temp5 = temp0(index)
    ·· DEBUG·· "temp5=",HEX2 temp5,CR
    ·NEXT

    FOR index = 0 TO 4·······················'debug shows 10 then 11 (didn't do all them)
    ··· temp0(index) = index +$10
    NEXT
    DEBUG "temp0=", HEX2 temp0,CR,"temp1=",HEX2 temp1,CR

    [noparse][[/noparse]/code]

    I think this will do what I want, neat trick. Thanks for your help.
    Bob
Sign In or Register to comment.