Ambiguity of slice limits?

BlitzMax Forums/BlitzMax Programming/Ambiguity of slice limits?

Phish(Posted 2004) [#1]
The documentation says this:

a=a[..200]      'resize a[] to 200 elements


But but the 200 is supposed to be an index, right? so shouldn't that resize it to 201 elements? I've been trying to use slices myself, and it doesn't seem too easy because of this ambiguity. Reading this suggests that the EndIndex isn't actually included in the resulting array:

StringOrArray[StartIndex..EndIndex]

The length of the returned slice is always EndIndex - StartIndex elements long


Say for example you do myArray[0..10], then it should be 10 elements long (EndIndex-StartIndex), yet if element number 10 was included, then it would be 11 elements long. Arrrg!


Phish(Posted 2004) [#2]
Ok, I did some investigation. And I don't really like the answer. My code:
Local a1[], a2[]
a1 = [1,2,3,4,5]
Print "Length is "+a1.length

a2 = a1[1..4]
Print "length of second array is "+a2.length
Print " - first element is "+a2[0]
Print " - last element is "+a2[a2.length-1]

And the results it produces:
Length is 5
length of second array is 3
 - first element is 2
 - last element is 4
Done.

Not very nice IMHO :-/


fredborg(Posted 2004) [#3]
a=a[..200]

Will make the slice go from 0..199, index ranges from 0 to size-1 as far as I understand.


Floyd(Posted 2004) [#4]
This is consistent with the way arrays are defined in BlitzMax, but different from classic Blitz.

For example, 'Local a[200]' creates a 200 element array, with elements a[0] to a[199].
The old style had 'Dim a(200)' creating a 201 element array with elements a(0) to a(200).

Slices a[0..100] and a[100..200] together form the slice a[0..200], which is the entirety of array a[].


Phish(Posted 2004) [#5]
I haven't got a problem with array indices ranging from between 0 to length-1 at all, I welcome the change. My problem is with the fact that if you do this:

a[start..n]

Then "start" is an index, while "n" refers to the n-th element, rather than the element with index n.

Actually, I don't have too much of a problem with this - I'd just like it to be clearer in the docs! It would be nice to be told that a[0..100] included element 0 but not element 100. (so it's neither 0..100 inclusive nor 0..100 non-inclusive)


ashmantle(Posted 2004) [#6]
I agree... its kinda mixed up like its now X(


bradford6(Posted 2004) [#7]
maybe this will clear it up some:

think of your array as a tube of cookie dough labelled a,b,c,d,e along the length of the tube. If you cut a SLICE after the A and another slice before the E you are left with B C D . right?

when you do an ARRAY[] = ARRAY[1..100] , you are actually making a copy and destroying the original. Blitz arrays are "Immutable", you cannot insert an index. if you want to insert an index you would destroy/create a new array.


Local a1:String[]=["a","b","c","d","e"]
Print "Length is "+a1.length

Local a2:String[]=a1[1..4] ' After Slice 1 and before Slice 4
Print "length of second array is "+a2.length
Print " - first element is "+a2[0]
Print " - last element is "+a2[a2.length-1]

'  a | b | c | d | e	     <==Array  
'    1   2   3   4  		<==Slices
'      ^   ^   ^              < == a1[1..4]