Array ()= Command

Purpose

Initializes an array from a string.

Syntax

Array ar() = string

ar():any array
string:string expression

Description

This command initializes an integer, floating point, string, or user-defined type array with the contents of a string with elements starting at 0 regardless of whether Option Base is set to 0 or 1; for a string array the input string is split at LF and CRLF characters. (LF = Chr(10) CRLF = Chr(13,10)).

Option Base 0

Dim fp$()

Array fp$() = "element 1"#10"element 2"#10"element 3"#10"element 4"

Print fp$(3) // Prints "element 4"

Option Base 1

Array fp$() = "element 1"#10"element 2"#10"element 3"#10"element 4"

Print fp$(3) // Also prints "element 4"

For a Byte array, Array ()= creates a one dimensional array with the same number of elements as the length of the string: each character is copied to an array element and the array has 0..Len(string) - 1 elements.

Dim b() As Byte

Array b() = "Testing"

Print CStr(b()) // Prints "Testing"

Before any array is created with Array ()=, it must first be declared using Dim or a similar command; note, however, that it is pointless defining the number of elements through this action as the Array ()= automatically re-dimensions the array to accomodate the data you allocate to it.

Dim f%(10)

Print Dim?(f()) // Prints 11

Array f() = Mki$(10, 4, 9)

Print Dim?(f()) // Prints 3

A user defined type array can also be initialized using this method, although the Type must be defined using Packed 1.

Type TestType Packed 1

by As Byte

in As Int

by2 As Byte

db As Double

EndType

Dim ar() As TestType

Array ar() = Mk1(1) + Mki(2) + Mk1(3) + Mkd(2.1) + Mk1(4) + Mki(5) + Mk1(6) + Mkd(7.1)

Print ar(0).by  // prints 1

Print ar(1).db  // prints 7.1

Example

OpenW 1

Global Dim mnu$()

Array mnu$() = "&File"#10 "&New"#10 "&Open"#10 "&Save"#10 _

"Save &As"#10 "-"#10 "E&xit"#10 #10 _

"&Edit"#10 "&Undo"#10 "-"#10 "Copy"#10 "Cut"#10 "Paste"#10 #10 _

"&Help"#10 "&About"#10 #10

Menu mnu$()

Do

Sleep

Until IsNothing(Me)

Remarks

Using Array()= to initialize an array or user-defined type is particularly useful with editor extensions, because the data can not be stored in Data lines.

$ = CStr(a())is the reverse of Array a()= $.

If a() is a byte array, CStr() creates a string of length Dim?(a|()) with the values of the elements of the array.

If a() is a string array, CStr() creates a string by adding all elements of the array and separating them with #13#10 (CRLF).

See Also

CStr()

{Created by Sjouke Hamstra; Last updated: 05/08/2019 by James Gaite}