BBC Basic Add an array to another array

SollyBunny

I'm trying to add an array to an array mainly using ARRAYLIB. Nothing I have done so far has worked

INSTALL @lib$ + "ARRAYLIB"
DIM ARRAY1(0)
DIM ARRAY2(3)
LET ARRAY2() = 1, 2, 3, 4

ARRAY1 is the main array with ARRAY2 being the array I want to append to ARRAY1. This is where I got stuck as everything I've tried didn't work.

PROC_add(ARRAY1(), ARRAY2())
PROC_add(ARRAY1(), (ARRAY2(0), ARRAY2(1), ARRAY2(2), ARRAY2(3))
PROC_add(ARRAY1(), ARRAY2(0), ARRAY2(1), ARRAY2(2), ARRAY2(3)
wizofwor

Proc_add doesn't append arrays as you expect. It adds a scalar value to all elements of an array.

 INSTALL @lib$ + "ARRAYLIB"
 DIM ARRAY(3)
 LET ARRAY() = 1, 2, 3, 4
 PROC_add(ARRAY(), 1) 

 DIM N% 0
 FOR N%=0 TO 3
   PRINT ARRAY(N%)
 NEXT

will produce an output like

2
3
4
5

In order to append two arrays you need to create a third one and to copy both arrays into that.

  DIM ARRAY1(0)                  ; 1 element
  DIM ARRAY2(3)                  ; 4 elements
  LET ARRAY2() = 1, 2, 3, 4

  N1% = DIM(ARRAY1(),1)          ; N1% = 0
  N2% = DIM(ARRAY2(),1)          ; N2% = 3
  DIM ARRAY3(N1%+N2%+1)          ; 5 elements

  FOR N% = 0 TO N1%              ; FOR N% = 0 TO 0
    ARRAY3(N%) = ARRAY1(N%)
  NEXT

  FOR N% = N1% TO N1%+N2%        ; FOR N% = 0 TO 3 
    ARRAY3(N%+1) = ARRAY2(N%)       
  NEXT

  FOR N%=0 TO N1%+N2%+1
    PRINT ARRAY3(N%)             ; Prints 0,1,2,3,4
  NEXT

Or you can write your own concatenate procedure as below:

  DIM ARRAY1(0)
  DIM ARRAY2(3)
  LET ARRAY2() = 1, 2, 3, 4

  PROC_Concat(ARRAY1(), ARRAY2(), ARRAY3())

  FOR N% = 0 TO 4
    PRINT ARRAY3(N%)
  NEXT

  END

  DEF PROC_Concat(A1(), A2(), RETURN A3())
  LOCAL N1%, N2%
  N1% = DIM(A1(), 1)
  N2% = DIM(A2(), 1)
  DIM A3(N1%+N2%+1)
  SYS "RtlMoveMemory", ^A3(0), ^A1(0), 10*(N1%+1)
  SYS "RtlMoveMemory", ^A3(N1%+1), ^A2(0), 10*(N2%+1)
  ENDPROC

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事