Shop OBEX P1 Docs P2 Docs Learn Events
Get an error when trying to use modifiers on array elements. — Parallax Forums

Get an error when trying to use modifiers on array elements.

Tom_WTom_W Posts: 4
edited 2012-03-14 20:03 in BASIC Stamp
I did a bit of searching on the forum but didn't find this exact problem. I'm not trying to alias into a specific nibble or a bit of an array of bytes, but simply use a modifier on an array element. If say arrBytes.NIB0(3) is legal and it accesses the byte array as if it was an array of nibbles (which is crafty and handy when needed) why is arrBytes(3).NIB0 an error? In my view this should be perfectly OK, but it isn't. What's the reason? Of course one walk-around is to first put the array element into a temporary variable and then use a modifier on it like in the example code below. Perhaps this could be a feature request for the next version.

The following example code will not run until offending lines are commented out.
' {$STAMP BS2}
' {$PBASIC 2.5}

arrBytes VAR Byte(8)
i VAR Byte

arrBytes(0)=$10
arrBytes(1)=$32
arrBytes(2)=$54
arrBytes(3)=$76
arrBytes(4)=$98
arrBytes(5)=$BA
arrBytes(6)=$DC
arrBytes(7)=$FE

DEBUG "arrBytes(3).NIB0=", HEX arrBytes(3).NIB0, CR 'GET ERROR: Expected ':' or end-of-line
DEBUG "arrBytes(3).NIB1=", HEX arrBytes(3).NIB1, CR 'GET ERROR: Expected ':' or end-of-line

'PUTTING PARENTHESES AROUND arrBytes(3) DOESN'T WORK EITHER.
DEBUG "(arrBytes(3)).NIB0=", HEX (arrBytes(3)).NIB0, CR 'STILL GET ERROR: Expected ':' or end-of-line

'HAVE TO ASSIGN arrBytes(#) TO A VARIABLE FIRST.
i=arrBytes(3)
DEBUG "i=arrBytes(3)=", HEX i, " i.NIB0=", HEX i.NIB0, CR 'THESE WORK OK.
DEBUG "i=arrBytes(3)=", HEX i, " i.NIB1=", HEX i.NIB1, CR

i=arrBytes(7)
DEBUG "i=arrBytes(7)=", HEX i, " i.NIB0=", HEX i.NIB0, CR
DEBUG "i=arrBytes(7)=", HEX i, " i.NIB1=", HEX i.NIB1, CR

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-03-14 20:03
    I don't know why the compiler does not like that, but that is definitely the way it is. Don't expect it to change! Parallax software is very stable. If I were writing the array, I would do it like the following using implicit arrays. More flexible:
    [SIZE=1][FONT=courier new]arrBytes VAR Byte
    arrNibs VAR arrBytes.nib0
    [/FONT][/SIZE][SIZE=1][FONT=courier new]arrByte0 VAR arrBytes[/FONT][/SIZE][SIZE=1][FONT=courier new]
      [/FONT][/SIZE][SIZE=1][FONT=courier new]arrByte1 VAR Byte[/FONT][/SIZE][SIZE=1][FONT=courier new]   ' implicit arrbytes(1)
    [/FONT][/SIZE][SIZE=1][FONT=courier new]arrByte2 VAR Byte[/FONT][/SIZE][SIZE=1][FONT=courier new]
    [/FONT][/SIZE][SIZE=1][FONT=courier new]arrByte3 VAR Byte[/FONT][/SIZE][SIZE=1][FONT=courier new]
    [/FONT][/SIZE][SIZE=1][FONT=courier new]arrByte4 VAR Byte[/FONT][/SIZE][SIZE=1][FONT=courier new]
    [/FONT][/SIZE][SIZE=1][FONT=courier new]arrByte5 VAR Byte[/FONT][/SIZE][SIZE=1][FONT=courier new]
    [/FONT][/SIZE][SIZE=1][FONT=courier new]arrByte6 VAR Byte[/FONT][/SIZE][SIZE=1][FONT=courier new]
    [/FONT][/SIZE][SIZE=1][FONT=courier new]arrByte7 VAR Byte[/FONT][/SIZE][SIZE=1][FONT=courier new]
      [/FONT][/SIZE][SIZE=1][FONT=courier new]i VAR Byte
    [/FONT][/SIZE]
    

    Having that, any element of the array is still addressable using arrBytes(n), for n=0 to 7, and also as arrNibs(2n) and arrNibs(2n+1). But also simply and directly as arrByte3 or arrByte3.nib0.
Sign In or Register to comment.