Array index from data?

Blitz3D Forums/Blitz3D Beginners Area/Array index from data?

Avrigus(Posted 2008) [#1]
Hello everyone,

I'm just wondering if it's possible define the index of an array based on the amount of values found in a data statement?

I have a list of 28 values in my data statement and I would like to use a function that returns said 28 values as an integer. This would be handy since it would get complex to update my code if I need to add or remove data values.

Is this possible to do with standard blitz commands?

Any help on this topic would be great :-)


markcw(Posted 2008) [#2]
Seems like it is.
size=Foobar()
Dim Array(size)
Array(size)=32

Print "size="+size+" Array(size)="+Array(size)
WaitKey()
End

Function Foobar()
 Return Rand(0,28)
End Function



Sledge(Posted 2008) [#3]
I would use a termination code in the data to allow you to count the elements...


Or you could go further and store the array size as element zero -- Blitz's non-standard way of dimensioning makes it particularly simple:

So you don't have to hard code the size of your array at all.

I'm not sure you can pass arrays as arguments to functions in B3D (bit rusty thanks to Max) so you might have to encapsulate a 'Blitz Array' in a type for that -- once you've taken that step you can record the array size as a field rather than element zero.


Avrigus(Posted 2008) [#4]
Awesome! Thank you very much for your help Sledge. your code makes perfect sense to me :-)


markcw(Posted 2008) [#5]
No, you can't pass arrays or static arrays as arguments in Blitz3D, good idea about encapsulation, I never thought of that. The obvious way is to use a bank.