array insert remove
BlitzMax Forums/BlitzMax Beginners Area/array insert remove| 
 | ||
| What is the easiest method of inserting and removing a value from an array? Eg. I have an array of x number of values, and now I want to add one and have the array dimension grow, or I want to remove one and have the array dimension shrink. [edit] forgot to mention that I want to add and remove values not just from the start and end of the array, but anywhere within the array. Otherwise slices should be good enough. | 
| 
 | ||
| There's a post from about a year ago where Mark suggested using listfromarray and back again in certain conditions and, in others, using a temp array. Can't find it now and can't think what search to use. | 
| 
 | ||
|  What is the easiest method of inserting and removing a value from an array? Use lists. | 
| 
 | ||
| You're probably right, seems like the only way to do it is with lists, unless I want the overhead of creating temporary arrays and do some for/next loops. So I'll take a look at changing my code to utilize lists instead. | 
| 
 | ||
| http://www.blitzbasic.com/codearcs/codearcs.php?code=1501 | 
| 
 | ||
| Not sure why I didn't spot that one. I will take a look at it, thanks Pertubatio. | 
| 
 | ||
| np :) | 
| 
 | ||
| I have made a function to insert a value into an array. It should be easy to convert it into float and other types of arrays. Thanks for the inspiration of your functions, now I can both remove and add values from within arrays. Local a:Int[] = [0,1,2,3,4,6,7,8,9] Print "Before: "+Len(a)+" fields" For Local b:Int = 0 To 8 Print a[b] Next a = insertIntoIntArray( a[..], 5, 5 ) Print "After: "+Len(a)+" fields" For Local c:Int = 0 To 9 Print a[c] Next Function insertIntoIntArray:Int[](Array:Int[],index:Int,value:Int) Local remain:Int = (Len(array)) - index Local result:Int[Len(array)+1] For Local count:Int = 0 To index-1 ' copy all values before insert result[count] = Array[count] Next result[index] = value ' insert value For Local count2:Int = 0 To remain-1 ' copy remaining values after insert result[index+count2+1] = Array[index+count2] Next Return result End Function | 
| 
 | ||
| zawran, check the length of your array before and after. Something is going wrong somewhere. I think it's the use of sizeof. Use len(array) instead. Also best to use... For Local d:Int = EachIn a Print d Next rather than a count field especially after using slices. | 
| 
 | ||
| You are correct. Using sizeof was a mistake. I have changed it to use len() and it should be working correctly now. I cannot use EachIn because I need to copy data from the beginning to an index and from the index to the end. EachIn would give me the entire array, or have I misunderstood something about the EachIn command? | 
| 
 | ||
| If you use the for x = 0 to 9 method and there are not you can get spurious values as it reads beyond the end of the array. This the code I came up with... 
Strict
Local a:Int[] = [0,1,2,3,4,6,7,8,9]
Print "Before:"
For Local b:Int = EachIn a
	Print b
Next
Print "Length : " + Len(a)
a = insertIntoIntArray( a, 5, 5 )
Print "Insert:"
For Local c:Int = EachIn a
	Print c
Next
Print "length : " + Len(a)
a = removefromintarray(a,5)
Print "Remove:"
For Local d:Int = EachIn a
	Print d
Next
Print "length : " + Len(a)
Function insertIntoIntArray:Int[](Array:Int[],index:Int,value:Int)
	Local remain:Int = (Len(array)+1) - index
	Local result:Int[Len(array)+1]
	For Local count:Int = 0 To index-1	' copy all values before insert
		result[count] = Array[count]
	Next
	result[index] = value				' insert value
	For Local count2:Int = 0 To remain-1	' copy remaining values after insert
		result[index+count2+1] = Array[index+count2]
	Next
	Return result
End Function
Function removefromintarray:Int[](array:Int[],index:Int)
    Local result:Int[Len(array)-1]
    Local remain:Int=index+1
    For Local count:Int = 0 To index-1
        result[count] = array[count]
    Next
    For Local count:Int = index+1 To Len(result)
       result[count-1] =  array[count]
    Next
    Return result
End Function
Any improvements let me know. | 
| 
 | ||
| Ah , you ment the code I had to test the functions with. I thought you were talking about the loop code within the functions. Then it all makes sense :) | 
| 
 | ||
| using slices ( cant think how to copy from x to y and retian the prevision values ) | 
| 
 | ||
| I thought about using slices but thought it'd be quicker to create the array the right size to begin with rather than resize it. Not sure it matters. When I tried to use slices to copy the remaining values I got '0'. Might have done something wrong but that's when I used the other method. |