Negative String/Array Slices
BlitzMax Forums/BlitzMax Module Tweaks/Negative String/Array Slices
| ||
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 |
| ||
Bumping this for Pert, nevermind it otherwise. |
| ||
myArray[-10..0] in your module is the same as myArray[length(myArray)-10 .. length(MyArray)] |
| ||
see! I'm not the only one! |
| ||
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. |
| ||
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. |
| ||
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! |
| ||
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. |
| ||
this is good, its cool, add it XD or the boys will be around |
| ||
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! |
| ||
Hope this gets added soon. |
| ||
I've been wanting this for a while. Thanks Noel! |
| ||
Bumpetty bump. |
| ||
I have come for your leader. |
| ||
The pants are strong with you. |
| ||
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? |