brainfade with slicing

BlitzMax Forums/BlitzMax Beginners Area/brainfade with slicing

Grey Alien(Posted 2007) [#1]
Hi, I'm not getting the expected results with slicing:

Strict
Local test[4]
test[0] = 10
test[1] = 20
test[2] = 30
test[3] = 40

Print Len(test)

test = test[1..2]

Print Len(test)


I'm expecting to see an array of length 2 but I get 1!

If I do test=test[2..2] I'd expect a single array slot but I get a length of 0, weird.

What am I doing wrong please?

Oh, also, is slicing 1-based? I assumed so as I thought 0..0 was a was to clear an array (instead of array=null)


tonyg(Posted 2007) [#2]
From the doc
The length of the returned slice is always EndIndex - StartIndex elements long
does this explain it?


JazzieB(Posted 2007) [#3]
When slicing the upper limit is ONE MORE than you want to keep in the array. Think of it as the point where you want the array sliced from, so it it actually gets chopped from that point forward.

This one got me initially, as well.


Grey Alien(Posted 2007) [#4]
Tonyg: yeah that helps thanks, I couldn't find slicing in the docs.

JazzieB: got it!

Also it seems that slicing is zero based after I did some testing.


Floyd(Posted 2007) [#5]
Slices are in the Language reference.


Grey Alien(Posted 2007) [#6]
thx