Negative String/Array Slices

BlitzMax Forums/BlitzMax Module Tweaks/Negative String/Array Slices

N(Posted 2005) [#1]
Edit: Oh yes, I'd like to ask Mark to -please- incorporate these changes into the BlitzMax source code.

Edit 2: Added String.Truncate method (*Removed*)

Edit 3: Added a quick mod to Truncate so it can accept negative arguments (truncates off the beginning of strings).

First off, thanks to (Master) Beaker for helping with this- he came up with the string slicing code first (two lines that would forever change slicing!). Anyhow, I applied a few modifications to his changes.

Anyhow, I applied the string slice modifications to the Array modifications so now you can do myArray[-10..0] and get the last 10 elements of an array.

Now one change to the way this works is that if you specify 0 as the ending point of the array/string and the first point is negative then 0 will be set to the ending of the array (because 0-(len-10) would return a blank string, so I'm putting this option to use).

Alright, here are the changes- to make this easier, I'm pasting the entire source files so you won't have to worry about putting the changes in the right place.

CHECK BELOW


N(Posted 2005) [#2]
Bumping this for Pert, nevermind it otherwise.


Dreamora(Posted 2005) [#3]
myArray[-10..0] in your module is the same as

myArray[length(myArray)-10 .. length(MyArray)]


Perturbatio(Posted 2005) [#4]
see! I'm not the only one!


N(Posted 2005) [#5]
myArray[-10..0] in your module is the same as

myArray[length(myArray)-10 .. length(MyArray)]


That's the idea... it's just making it shorter so you don't have to type stuff you shouldn't have to.


N(Posted 2006) [#6]
Updated for v1.16:

blitz_array.c


blitz_string.c


These files are under mod/brl.mod/blitz.mod

They [i]shouldn't be OS dependent and from what I can tell they aren't. These changes will not affect existing code in any way. All they do is add relative slices to strings and arrays. This allows you to use negative values in place of [(arr.Length-begin)..(arr.Length-end)] and instead just do [-5..] (<- gets the last five elements in the array).

Anyone who uses Python or Ruby probably misses this feature to an extent. I know I do. That's why I added it in the first place. And now I'm adding it again. So have at it.


Jay Kyburz(Posted 2006) [#7]
So if you do this myArray[-12..6] will you get the elements in the array 12 from the end and 6 from the start.

and myArray [1..-1], will this trim the start and end characters? That would be cool.

Make it official!


N(Posted 2006) [#8]
Jay: That is correct. It's very good at saving time typing since you don't have to go "myArray.Length - 12".

I had also added a Truncate method, but apparently my definition and Mark's differ, so that was a no-go.


boomboommax(Posted 2006) [#9]
this is good, its cool, add it XD or the boys will be around


Jay Kyburz(Posted 2006) [#10]
I think this would be great.

I don't like making changes to my mods because i work on at least 3 machines. It's a pain to keep them updated so I really hope this makes it into an official release.

Please!


Beaker(Posted 2006) [#11]
Hope this gets added soon.


TeaVirus(Posted 2006) [#12]
I've been wanting this for a while. Thanks Noel!


N(Posted 2006) [#13]
Bumpetty bump.


N(Posted 2006) [#14]
I have come for your leader.


N(Posted 2006) [#15]
The pants are strong with you.


Beaker(Posted 2006) [#16]
I'd still really like to see this become part of the official mod. Lots of other languages have 'negative' slicing like this, why not BlitzMax?