BBC Basic将一个数组添加到另一个数组

时间:2019-06-20 21:10:56

标签: arrays basic

我正在尝试主要使用ARRAYLIB将数组添加到数组。到目前为止,我没有做过任何事情

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

ARRAY1是主数组,而ARRAY2是我要附加到ARRAY1的数组。这是我被卡住的地方,因为我尝试过的所有方法都无法正常工作。

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)

1 个答案:

答案 0 :(得分:0)

Proc_add不会按预期附加数组。它将标量值添加到数组的所有元素。

 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

将产生类似

的输出
2
3
4
5

要追加两个数组,您需要创建第三个数组并将两个数组复制到该数组中。

  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

或者您可以编写自己的连接过程,如下所示:

  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