Appending Arrays

BlitzMax Forums/BlitzMax Beginners Area/Appending Arrays

Is there a way to append to arrays without wiping the array clean?

Basically doing what: Array:String = new String[NewNumElements] does, without wiping the array.

I've tried Array = Array[..NewNumElements] But it doesn't seem to be working.

you don't have an array so how should it work?

It would need to be

someStringArray:string[] = ...

cause thats all that was wrong

Do you just want to append new entries to an existing array or do you want to concatenate (join) two arrays, one on the end of the other? There was a recent update which allowed array concatenation, I think.


Global A:String[]=["One","Two","Three"]
Global B:String[]=["Four","Five","Six"]
Global C:String[]=A+B

For Local I:Int=0 To 5
	Print C[I]
Next


Dreamora, I'm not sure what you mean. The first example:
Array:String = new String[NewNumElements]
works. The full code would be:
Local Array:String[30]
Local NewNumElements:Int = 20
Array:String = New String[NewNumElements]


Gabriel: Appending new entries, not concatenation.

The original code was:
	Method AddFunction(FunctionName:String) 
		NumFunctions:+1
		Functions = Functions[..NumFunctions] 
		Functions[NumFunctions - 1].Create("Function", FunctionName) 
	End Method

But its giving me a accessing null field error.

Appending new entries, not concatenation
Can you explain the difference?

As far as I can see, he's exactly answered your question.

GFK: I guess I was a bit hasty in my assumption there. I want to add a blank field to the existing array rather than concatenating two different arrays together.

Edit:
Tried Concoctenation:
	Method AddFunction(FunctionName:String) 
		Local TempArray_A:TCode[] = Functions
		NumFunctions:+1
		Local TempArray_B:TCode[] = New TCode[NumFunctions] 
		Local TempArray_C:TCode[] = TempArray_A + TempArray_B
		Functions = TempArray_C
		Functions[NumFunctions - 1].Create("Function", FunctionName) 
	End Method

same problem as before.

then your code is just wrong, point

SuperStrict

Global Functions :String[30]
Global NumFunctions:Int = 20
Functions = New String[NumFunctions] 

addfunction("test")
addfunction("gaga")
addfunction("i just did it wrong again!")
Delay 2000

End

Function AddFunction(FunctionName:String) 
		NumFunctions:+1
		Functions = Functions[..NumFunctions] 
		Functions[NumFunctions - 1] = FunctionName
		Print Functions[NumFunctions - 1]
End Function


works without any problems.
Most likely because you put the :string behind it thought it is no :string, its :string[] which is something elementally different (actually a different class! :Array)

I want to add a blank field to the existing array

array=array[..len(array)+1]
Local array:String[]=["one","two","three"]
Local len1:Int=Len(array)
array=array[..len1+1]
Local len2:Int=Len(array)
Print len1 + " " + len2
array[3]="Four"
For Local all:String=EachIn array 
	Print all
Next