Adding/removing array elements?

BlitzMax Forums/BlitzMax Beginners Area/Adding/removing array elements?

Drekinn(Posted 2005) [#1]
How the heck do I add or delete values to and from an array?? Or is it not possible? Eg. numArray = [1,2,3,4,5]


degac(Posted 2005) [#2]
Why don't use list? Is the easiest solution: you can add, remove and move item without pain.

PS: Merry Christmas!


Perturbatio(Posted 2005) [#3]
extending an array can be done with a slice:
Global numArray:Int[] = [1,2,3,4,5]
numArray = numArray[..7]
for Local i:Int = eachin numArray
   print i
next


you can't concatenate array slices however, so removing an element involves looping through the array, from the index you want to remove and copy the value in the next element to the current

I stuck some concatenate array functions in the code archives that might help though.


Drekinn(Posted 2005) [#4]
degac,
I tried that at first, but realised I only need to create a list of integers, as opposed to objects. Can someone please show me how to achieve this?

Perturbatio,
Hmm... slices could well be the answer. I'll do some experimentation. Thanks for the help.


Perturbatio(Posted 2005) [#5]
take a look at the BlitzWiki for more on slices.


degac(Posted 2005) [#6]
mmm. You want a list ONLY of INT: I think LIST in Bmax handle generic object...unless you write a 'personal' tlist. Or you can 'force' like in the following example:

Global mylist:tlist=New tlist
Global myArray_cont=0' count how many elements do yoy have - not necessary

Type myarray
	Field id
	Field val:Int' <--- you declare the type as INT

Function Add:myarray(valore:Int)
		Local e:myarray=New myarray
		e.id=myArray_cont		
		e.val=valore
		ListAddLast mylist,e'       add the element to a list
		myArray_cont:+1
		Return e
End Function

Function Show()
For Local a:myarray=EachIn mylist
Print "["+String(a.id)+"] = "+String(a.val)
Next
End Function


End Type

For i=1 To 5
myarray.add(i)
Next 
myarray.show()

WaitKey
End


Hope this can be useful. It's based upon generic list of Bmax - using type to add each element


Perturbatio(Posted 2005) [#7]
I just converted my stringlist code and created an IntList:


It's faster that a list for most things or comparable for others.

There may still be some teething issues, but it seems to work.


Drekinn(Posted 2005) [#8]
Thanks degac and Perturbatio for your help, but I think your methods are unduly complex for such a simple task as adding/removing a couple of numbers from a list. I truly appreciate your assistance though. :)


Cajun17(Posted 2005) [#9]
Depending on the size of the list and what it's used for you may need a very complex list object.

If the order doesn't matter you can remove an item by replacing it with the last element and then slicing it.
Function Delete(num:Int)
    array[num] = array[array.length - 1]
    array = array[..array.length - 1]
EndFunction

Another solution if you access the array by looping through it you can mark the ones that you want to delete some how, maybe make them negative?
Local marked:Int = 0
For Local i:Int = 0 To array.length - 1
    If array[i] < 0 Then
        If i < array.length - 1 Then
            array[i] = array[i +1]
            array[i + 1] = -array[i + 1]
        End If
        marked:+1
    EndIf
    'Do Logic here
Next
If marked > 0 Then array = array[..array.length-marked]

The easiest solution would probably be to either wrap the Ints in objects or use Perturbatio's List since inserting and removing from the middle is the point of a linked list.