[Solved] Some questions regarding dynamic arrays.

BlitzMax Forums/BlitzMax Beginners Area/[Solved] Some questions regarding dynamic arrays.

Grisu(Posted May) [#1]
Hello all!

I'm reading in a large textfile into a dynamic string array and split it linewise.

A. Is there a way to determine the number of array elements without having to go through all of them step by step?

B. Is it possible to delete the very last element of the array? In my case it's always an empty line / data element and I don't want to check for it every time.


Example code
	Local line_counter:Int=0
        Local l:String

	Local k:String[] = LoadText("mytext.txr").split("~n")

	For l:String = EachIn k
         If l <> "" Then line_counter=line_counter+1 
	Next 


I'm looking for some basic tweaks, perhaps there's a smarter way of doing this. ;)

Thanks!
Grisu


col(Posted May) [#2]
Hiya,

A.
You can use the '.length' member of arrays. So you would have

For i:Int = 0 Until k.length
    Local l:String = k[i]

Don't forget that arrays are 0 indexed - you would need to iterate from 0 to array.length - 1, or use Until as above.


B.
Using the array length above is it ok to just ignore that last element? with k.length - 1 or '- whatever' as needed.


Grisu(Posted May) [#3]
Thank you Dave. Works fine and saves some ms.


Henri(Posted May) [#4]
Hi,

just for fun,

Array "AddLast" method:
array = array[..array.length + 1]
array[array.length - 1] = "New value" 'Assuming array is of strings

Array "DeleteLast" method:
array = array[..array.length - 1]

-Henri