I assume this kind of slicing isn't possible?

BlitzMax Forums/BlitzMax Beginners Area/I assume this kind of slicing isn't possible?

SculptureOfSoul(Posted 2007) [#1]
Well, what i'd like to do is something like this

array1[10..110] = array2[ 20..120 ]

But I'm getting a "can't linearize reference" error. I assume the only way to copy elements like this from one part of one array to a specific section of another array is to do it in a loop. Am I correct?


Perturbatio(Posted 2007) [#2]
yes


Dreamora(Posted 2007) [#3]
Not possible at the moment. As seen in Marks worklog, it might become possible as you can concatenate slices and therefor could replace parts within Array1 with the parts in Array2


SculptureOfSoul(Posted 2007) [#4]
Thanks for the quick reply folks ;).


ImaginaryHuman(Posted 2007) [#5]
For Local Count:Int=10 to 109
array2[Count+10]=array1[Count]
Next


SculptureOfSoul(Posted 2007) [#6]
Thanks AngelDaniel, although actually the example I posted isn't exactly what I want to do, but similar enough.

What I wanted to do was to take an array, and then at an create another array one longer than the original, and copy the entire first array into the second array, but splitting it at some element specified as a parameter, and adding one additional element.

So basically, the operation I wanted was something like
Array1
Array2 = new array[Array1.length + 1]

Array2[0..x] = Array1[0..x]
Array2[x] = Some_New_Element
Array2[x..] = array1[x..]

I've got it working, it's just a bit more of a hassle than I would have liked. I love Blitzmax but after learning Lisp stuff like this makes me groan - in Lisp it would be trivial to change the language itself so that you could do the above operation by typing it out like I did, and then behind the scenes it gets translated to the code that I wrote (all of that done pre-compilation, so in the end it would be just as if I had written the longwinded code I wrote, but with the beauty and simplicity of the above syntax).

Oh well, Lisp doesn't have the libraries to make it suitable for game development as quickly or rapidly as Blitz, so I can't complain. I just always want to have my cake and eat it too. :P


Dreamora(Posted 2007) [#7]
thats about the worst idea you can have.

resizing by one ... the overhead if you do this over and over again is massive and will cause massive performance lose. (slicing actually has to copy the data manually in case of objects, it even means that it has to do the whole GC involved stuff)
Unless you are sure its only a single attempt, always do resizing on *2 /2 base or similar large (enough free space for further operations) and just ignore the unneeded part until you finished your operations and can be sure that it will remain as it is.

Alternative use Array to TList, add there and cast back ... but that would cast it to common object not to a specific class.


Grey Alien(Posted 2007) [#8]
Yeah I needed to dynamically resize an array and I made it resize in chunks every so often, a bit like allocated memory sometimes works.