Finding the length of an array

BlitzMax Forums/BlitzMax Beginners Area/Finding the length of an array

TrickyRic(Posted 2012) [#1]
HI,

I'm extremely rusty with BlitzMax and I'm starting to convince myself that the language has changed since it's documentation was written!

How do I find the length of an array that's defined as myArr:Int[][] ? I've found references to numberOfDimensions() which seems to not exist, as well as references to myArr.Dimensions:Int() and myArr.Dimensions() but neither are working.

I want to grab the length of both myArr and myArr[x] so that I can do something like this:

Local row:Int = 0
Local col:Int = 0
For row = 0 To arrVertices.Dimensions:Int()
For col = 0 To arrVertices[row].Dimensions:Int()
' Do stuff
Next
Next

Can somebody please drop me a hint as to where I'm going wrong?

Regards.


Kryzon(Posted 2012) [#2]
I think JoshK wanted to know something like this as well, and he realized there's a read-only 'length' field:

arrVertices.length:Int()

arrVertices[row].length:Int()


TrickyRic(Posted 2012) [#3]
Hi Kryzon,

Thank you, that's exactly what I was looking for. Always simple in the end!

Regards.


TrickyRic(Posted 2012) [#4]
Now I'm stumped as to why this isn't working:

Local myArr:Int[][]
myArr[0][0] = 1

I understand that myArr is currently empty so technically myArr[0] doesn't exist yet, thus neither does myArr[0][0], but how do I grow myArr so that [0][0] does exist to be populated?

Regards.


col(Posted 2012) [#5]
Hiya,

Try it this way..



As far as getting the length - you know this already when you size the array, no?

Last edited 2012


Kryzon(Posted 2012) [#6]
Also look here: http://en.wikibooks.org/wiki/BlitzMax/Language/Arrays#Resizing_an_Array_of_Arrays

At first myArr is an empty array of arrays. You need to create elements for it.
I'm not sure if this is correct, but try this:
Local myArr:Int[][]

myArr = Int[10][] 'First create the top array.

myArr[0] = Int[10] 'then create the children arrays.
myArr[1] = Int[10]

myArr[0][0] = 1
'...


Last edited 2012


TrickyRic(Posted 2012) [#7]
Hi Col,

Unfortunately while populating this array, I don't have any means of knowing how large it's going to end up, or how large each of the child arrays is going to be so I can only increment the lengths as I go.

What I'm basically doing is loading a text file into arrays. Using readline() I'm grabbing each row, then splitting by the space charactors and using those as columns. I.e. myArr[1][3] would be row 2 of the txt file, column 4 of it's content.

Could you give me an example of how I could apply the myArr = New Int[x,y] or [x][y] where y could vary as x is looped please?

Sorry to be a pain!

Regards.


col(Posted 2012) [#8]
Hiya,

Personally I'd use a different approach as BlitzMax doesn't like resizing arrays of arrays how you're describing. You could have a Type called TDocumentLine which will hold an array for each line of text which will be split by a space character, then have an array of these types. Both can then be resized and read individually :-


Be aware this is completely untested and just brain-stormed and may not even run as is, and not even be what you're after -


Last edited 2012


TrickyRic(Posted 2012) [#9]
Hi Col,

Not exactly what I was looking for no, but your [..index + 1] method did the trick - thank you!

I take it then that arr[..i+1] is the array-element equivalent of var:+1 ?

Regards.


col(Posted 2012) [#10]
it is yes, when 'index' is the original array length.