Splicing two arrays together

BlitzMax Forums/BlitzMax Programming/Splicing two arrays together

Grey Alien(Posted 2006) [#1]
So what I'd really like to be able to do is splice two arrays together with an easy command (guess I'll have to make one) so that I can do this sort of thing to easiy remove an array slot and shuffle the other values up:

Local a[3]
a[0] = 1
a[1] = 2
a[2] = 3

a = a[0..1] + a[1..3]


of course the code above won't compile, but I'd love to end up with an array 2 slots long with values of 1 in slot 0 and 3 in slot 1.


tonyg(Posted 2006) [#2]
Do you mean array concatentation or array insert/remove ?


Grey Alien(Posted 2006) [#3]
both thanks Tony. Actually mine was for strings and I just wrote a function that removes a range of values from a string array. I'll post it when I've fixed it. I just found out that slices are 1-based not zero-based!!


Grey Alien(Posted 2006) [#4]
here it is:

Function ccRemoveStringArraySlots:String[](array$[], StartSlot%, EndSlot%)
	'Slots passed in should be zero-based.
	'Smaller array is returned.
	Local temp$[] = array[0..StartSlot] 'first get the first chunk (note that array slicing is 1-based)
	'Grow temp by the correct size.
	temp = temp[0..(Len(temp)+(Len(array)-endslot))-1]
	
	'Now loop through the remainder of the array
	Local addto = StartSlot
	For Local i=EndSlot+1 To Len(array)-1
		temp$[addto] = array[i]
		addto:+1
	Next	
	
	Return temp
End Function


Now I can write a similar concatenation function although I'm not sure I need it any longer.


tonyg(Posted 2006) [#5]
For strings or an array containing strings?
For strings can't you use mid$?
For array of strings isn't it the same concatentation code?


Grey Alien(Posted 2006) [#6]
An array of strings, yeah the int code from the link would work, thanks. Yeah I've known about Mid$ since I first made a scrolling message at the age of 8 on a Spectrum ;-p


dmaz(Posted 2006) [#7]
fyi: strings have full slicing capability... this works just fine.
Local s:String = "12345"
s = s[..1] + s[2..]
Print s

imo, nicer than mid