slicing arrays

BlitzMax Forums/BlitzMax Beginners Area/slicing arrays

Bremer(Posted 2005) [#1]
Is there an easier way to remove one value in an array than doing a for/next loop.

eg.

If I have an array vx:int[256] and want to remove the value stored at location 20 and move all following values one place down, how would you do it?


Tom Darby(Posted 2005) [#2]
Not that I'm aware of, no--you'll need to step through and bump everything down.

Some thoughts:

Have you considered using a list instead of an array, or is this a fairly minor issue?

Have you considered simply setting this entry of the array to null and ignoring null values when working with the array (except for insertions)?


ImaginaryHuman(Posted 2005) [#3]
Can't you do:

originalarray:int[256]
newarray:int[20]=originalarray[0..20]
newarray[20..255]=originalarray[21..256]

???


TeaVirus(Posted 2005) [#4]
Are you looking to remove the element and shrink the array? Not sure exactly what you're trying to do but if the order of your elements doesn't matter then maybe you could do this:
vx[20]=vx[256]
vx=vx[..(vx.length-1)]



Bremer(Posted 2005) [#5]
Using lists would work, but I don't want to overly complicate things. I need a type to hold two sets of up to 256 values. So I figured that arrays would be the simplest to use.

What AngelDaniel suggest, I can't get to work, keep getting different kinds of errors regardless of how I set it up. So I am thinking that Tom is right in having to stick with the for/next loop, which I have done and it works fine. I was just looking to see if there were an easier way, which there might not be.


Jay Kyburz(Posted 2005) [#6]
Hey Angel, Thats a great tip. I've never though about doing that. I wonder it its much faster that pushing every element down and trimming the end. Uses more memory but thats not even close to being an issue in my game.


Azathoth(Posted 2005) [#7]
What compiler is Angel using? I'm getting compiler errors.


Jay Kyburz(Posted 2005) [#8]
yeah.. i've been playing around with it tonight and i don't thing you can actually do this,


ImaginaryHuman(Posted 2005) [#9]
Um, sorry, that's not exactly correct syntax. It's was a question, not a suggestion.

I don't think you can do `part of an array equals part of another array`.

I was asking if it were possible.


Bremer(Posted 2005) [#10]
I don't think that it is. I haven't found a way where it works anyways.


Dreamora(Posted 2005) [#11]
It is not possible to define a "slice input target" so far, although it has been requested since day one of slicing because it takes out one of the main potential usages of slicing.

If you know, that you will remove items often, Lists are the best option anyway.
Arrays are only good for "static" datasets that don't need a large amount of reorganisation and "insert at / remove at" functionality.