Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fill integer array
#1
Hi Gintaras,
Is there any way to fill an integer array as easily as it is to fill a one-dimensional string array by setting it equal to a str collection.
I just though of val(_s) technique - see below. But this is still longer than a single set statement.
Thanks, S


Code:
Copy      Help
str StringColl = "One[]Two[]Three[]Four[]Five"
ARRAY(str) arrStringVars = StringColl
for _i  0 arrStringVars.len
,out arrStringVars[_i]
,
;Integer Array = 1 2 3 4 5
ARRAY(int) arrIntVars
arrIntVars[] = 1
arrIntVars[] = 2
arrIntVars[] = 3
arrIntVars[] = 4
arrIntVars[] = 5
out "[]"


for _i  0 arrIntVars.len
,out arrIntVars[_i]
,
arrIntVars.redim

str strIntColl = "1[]2[]3[]4[]5"
foreach _s strIntColl
,arrIntVars[] = val(_s)
out "[]"
for _i  0 arrIntVars.len
,out arrIntVars[_i]
#2
Function ArrayIntFill
Code:
Copy      Help
;/
function ARRAY(int)&a [e0] [e1] [e2] [e3] [e4] [e5] [e6] [e7] [e8] [e9] [e10] [e11] [e12] [e13] [e14] [e15] [e16] [e17] [e18] [e19] [e20] [e21] [e22] [e23] [e24] [e25] [e26] [e27] [e28] [e29]

;Creates int array with up to 30 elements.

;a - array variable.
;e0...e29 - element values.

;EXAMPLE
;ARRAY(int) a
;ArrayIntFill a 5 8 2
;int i
;for(i 0 a.len) out a[i]


int i n=getopt(nargs)-1
int* p=&e0
a.create(n)
for(i 0 n) a[i]=p[i]
Function ArrayIntFill2
Code:
Copy      Help
;/
function ARRAY(int)&a $values

;Creates int array and fills with values specified in string.

;a - array variable.
;values - element values, like "4 -2 85". Separators can be spaces, tabs, new lines, commas.

;EXAMPLE
;ARRAY(int) a
;ArrayIntFill2 a "4 -2 85"
;int i
;for(i 0 a.len) out a[i]


ARRAY(lpstr) b
tok values b -1 " [9][],"
int i n=b.len
a.create(n)
for(i 0 n) a[i]=val(b[i])
#3
Thanks both work great!
Just so I understand syntax on first one:

the integer pointer (*) variable p initially points to the address (&) of the first argument e0.

I am a little unsure what the [i] after the p means - is it incrementing the pointer to the address in memory by 1 to pull in the additional arguments.

I see from the help menu


Code:
Copy      Help
A single character can be accessed using [] operator:

;

str s = "Cat"
int char = s[1]
;now char is 97 ('a' character code)
s[0] = 66
;now s is "Bat" (66 is 'B' character code)
s[2] = 'r'
;now s is "Bar"


but why is the i-th character in the address of e0 equivalent to the next argument's value?

Thanks,
S
#4
Operator [] gets array elements. A string in QM usually is an array of bytes.

Function parameters are like simple array on stack. If int* p=&e0, then p[0] is &e0, p[1] is &e1 and so on.
#5
now makes sense. Thanks so much!
Where does this programming convention come from...is this from memory management in C or is it unique to QM?
I don't know the C family of programming languages but I have read that one of the major differences between C and the others C++, C#, etc is that this memory management/garbage collection is handled automatically.
S
#6
Operator [], pointers and function parameter placement are the same as in C and C++.
QM2 from the beginning was designed to be able to use Windows API functions like in C and C++.
Automatic memory management like in C++, without garbage collection.
#7
Thanks for explanation. Always more to learn!
S


Forum Jump:


Users browsing this thread: 1 Guest(s)