Ever need to store more than one datatype in an array? You can do it with strings.
For this, we will be making an array to store a person's name, age, and height (meters), using strings, integers, and reals.
In DarkBasic, datatype indicators are as follows:
String: $
Real: #
Integer: None, but we will use ! in this tutorial.
We will do this for 5 people, and are storing three subdimensions.
We will be using a string datatype for the array.
Now we need to apply names to the people. Subdimension 1 will store names.
In order to later determine what datatype is used, we will add a prefix to all strings in this array. Since the name will be a string, we will use the $ prefix. The prefix is the first character in the string.
person$(1,1)="$Josh"
person$(2,1)="$John"
person$(3,1)="$Bob"
person$(4,1)="$Bill"
person$(5,1)="$Dave"
Next, we will define the age of the people using integers with the prefix !, but we will still enter data as strings.
person$(1,2)="!12"
person$(2,2)="!14"
person$(3,2)="!16"
person$(4,2)="!18"
person$(5,2)="!20"
The last data we will enter is the height in meters of the people. This will be done in real numbers with the # prefix.
person$(1,3)="#1.5"
person$(2,3)="#1.6"
person$(3,3)="#1.7"
person$(4,3)="#1.8"
person$(5,3)="#1.9"
Finally, we retrieve data from the array in the correct datatype. You can either code a method yourself or use the functions below.
Determine the datatype:
function extract_type(complex$)
if left$(complex$,1)="!" then value=1
if left$(complex$,1)="#" then value=2
if left$(complex$,1)="$" then value=3
endfunction value
If the prefix indicates an integer, the value is 1; if real, 2; if string, 3. If none of these are present, the value is 0.
function cstring(complex$)
value$=right(complex$,len(complex$)-1)
endfunction value$
function cint(complex$)
value=val(right$(complex$),len(complex$)-1))
endfunction value
function creal(complex$)
value#=val(right$(complex$),len(complex$)-1))
endfunction value#
All that's left to do is print this data to the screen. Your final code should look something like this:
`declare array
dim person$(5,3)
`declare names
person$(1,1)="$Josh"
person$(2,1)="$John"
person$(3,1)="$Bob"
person$(4,1)="$Bill"
person$(5,1)="$Dave"
`declare ages
person$(1,2)="!12"
person$(2,2)="!14"
person$(3,2)="!16"
person$(4,2)="!18"
person$(5,2)="!20"
`declare heights
person$(1,3)="#1.5"
person$(2,3)="#1.6"
person$(3,3)="#1.7"
person$(4,3)="#1.8"
person$(5,3)="#1.9"
`print data to screen
for q=1 to 5
for w=1 to 3
if extract_type(person$(q,w))=1 then print cint(person$(q,w))
if extract_type(person$(q,w))=2 then print creal(person$(q,w))
if extract_type(person$(q,w))=3 then print cstring(person$(q,w))
next w
next q
`functions
goto endoffunctions
`extract datatype
function extract_type(complex$)
if left$(complex$,1)="!" then value=1
if left$(complex$,1)="#" then value=2
if left$(complex$,1)="$" then value=3
endfunction value
`extract string
function cstring(complex$)
value$=right(complex$,len(complex$)-1)
endfunction value$
`extract integer
function cint(complex$)
value=val(right$(complex$),len(complex$)-1))
endfunction value
`extract real
function creal(complex$)
value#=val(right$(complex$),len(complex$)-1))
endfunction value#
endoffunctions:
