Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Initialize an Array?
#1
Pretty basic question, but couldn't find answer in help or forum ...

What is the simplest, most direct way to initialize the contents of an array.

For example, if I have the following array:

array[0 0] = 5
array[0 1] = 7
array[1 0] = 2
array[1 1] = 12
array[2 0] = 1
array[2 1] = 4

is there a simple, one line method of initializing the array to these values?
#2
It is easy to initialize string arrays. Example:

ARRAY(str) a
a="a[]b[]c"

In other cases, each element must be initialized in separate statement, as in your example, or using for.
#3
My application is for an integer array, with no formula or equation to use in a for loop. I suppose I could load the data into a string array (or variable), then use "for" to parse that string into integers and load the array?
#4
Function PopulateIntArray:

Code:
Copy      Help
;/
function ARRAY(int)&a str's [dim1]

;Converts string containing integer values to array of integers.
;If dim1 is omitted or 0, creates array of single-dimension. If dim1
;is greater than 0, creates array of two dimensions, where length of
;first dimension is dim1.


;EXAMPLES
;int i j
;ARRAY(int) a
;
;out "-- Single dimension --"
;PopulateIntArray a "5 6 7"
;for(i 0 a.len) out a[i]
;
;out "-- Two dimensions; length of first dimension is 3 --"
;PopulateIntArray a "5 7, 2 12, 1 4" 3 ;;commas here are used only to make the code easier to read
;for(i 0 a.len(1))

,;for(j 0 a.len(2))
,,;out "a[%i %i] = %i" i j a[i j]


ARRAY(lpstr) la
tok s la -1 "" 1
if(!la.len) end ES_BADARG

int i j k dim2
if(dim1>0)
,dim2=la.len/dim1; if(!dim2) end ES_BADARG
,a.create(dim1 dim2)
,for i 0 dim1
,,for j 0 dim2
,,,a[i j]=val(la[k])
,,,k+1
else
,a.create(la.len)
,for i 0 la.len
,,a[i]=val(la[i])
#5
Excellent ... Thanks!


Forum Jump:


Users browsing this thread: 1 Guest(s)