Filling arrays

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is sometimes necessary to fill arrays with some known values.
The most straightforward method is:
B4X:
array(0) = 2
array(1) = 3
...
For larger arrays this method is a little cumbersome.
A much cleaner solution is to use StrSplit.
StrSplit splits a string and returns an array.

First we declare the array in sub Globals as an empty array.
Later we load the data using StrSplit:
B4X:
Sub Globals
    Dim data(0)
    Dim names(0)
End Sub

Sub App_Start
    data() = StrSplit("2,3,5,7,11,13,17,19,23" , ",")
    names() = StrSplit("John,Robert,Mike,Barbara" , ",")
    
    'Show all names
    For i = 0 To ArrayLen(names())-1
        Msgbox(names(i))
    Next
End Sub

Make sure that you don't leave any extra spaces inside the string.
 

grgczyz

Member
Licensed User
Filling 2-D arrays ... is it possible?

:sign0188:
I like this method very much however I wonder if it is possible to fill 2-D arrays similarly as 1-D arrays?
 
Top