Array Issues..  


Tweet
(Posted 1 month ago) hardcoal

Hi..
is there any way to remove an Array an item like you do with list..

Lets say i have Array["Shower","Spoon","Key"] and i want to remove the spoon only from the list..
is it possible?

Also how do i clear an array, with Null?

many things arent so clear with arrays to me..

cheers

(Posted 1 month ago) RonTek commented:

Im not really an expert when dealing with BlitzMax arrays, but you can try these code archives that I recently posted related to list and arrays

degac's
https://www.blitzcoder.org/forum/topic.php?id=888
Perturbatio IntList
https://www.blitzcoder.org/forum/topic.php?id=887

(Posted 1 month ago) degac commented:

This is a short snippet

(edited many times... no idea how to put code here!!!!)

Local arr:String[]="shower,spoon,key".split(",")
PrintArray(arr)
arr=ArrayRemoveByID(arr,1)
PrintArray(arr)
'second way, faster...
arr="alfa,beta,gamma,delta".split(",")
Local arr2:String[]
PrintArray(arr)
'remove the 2nd items (count from 0... 0=alfa, 1=beta...)
'2nd item = beta
arr2=arr[..1]+arr[2..]
PrintArray(arr2)
'how to clear an array
arr2=Null
PrintArray(arr2)
End

Function ArrayRemoveByID:String[](arr:String[],id:Int=-1)
    If id=-1 Return Null

    Local tmp:String[]
    Local cnt:Int
    For Local i:Int=0 Until Len(arr)
        If i<>id
            tmp=tmp[..cnt+1]
        tmp[cnt]=arr[cnt]
            cnt:+1
        End If  
    Next
    Return tmp
End Function

Function PrintArray:Int(arr:String[])
    For Local i:String=EachIn arr
        Print "["+i+"]" 
    Next
    Print "-------------------"
Return 0
End Function
  (Posted 1 month ago) degac commented:

New 'generic' snippet

Local arr:String[]="alfa,beta,gamma,delta".split(",")
Local arr2:Object[]'<----- generic container!!!!
PrintArray(arr)
arr2=ArrayRemoveID(arr,1)'object format <---- !!!!
Print "Len arr2 : "+Len(arr2)
PrintArray(arr2,"string") 'need to be converted
End

Function ArrayRemoveID:Object[](arr:Object[],id:Int=-1)
    If id=-1 Return arr
    If Id>Len(arr) Return arr
    Local arr2:Object[]
    arr2=arr[..id]+arr[id+1..]
    Return arr2
End Function

Function PrintArray:Int(arr:Object[],_class:String="string")
    For Local i:Object=EachIn arr
        If _class="string" Print "["+String(i)+"]"  
    Next
    Print "-------------------"
    Return 0
End Function
(Posted 1 month ago) RonTek commented:

Thanks degac! great solutions btw I have updated your code format so you can edit it and see how formatting works. cheers.

(Posted 1 month ago) degac commented:

Ahhhh! Thanks!
I read the help but I missed the need to specify the language after 3' ```blitzmax

Reply To Topic (minimum 10 characters)

Please log in to reply