Slices - clarification

BlitzMax Forums/BlitzMax Beginners Area/Slices - clarification

Emmett(Posted 2005) [#1]
Hello everyone,
As you can tell from this question I am Very New to this stuff.
In the language reference on page Slices my understanding is:

a=a[..] 'copy all elements of a[] "need clarification here"
Am I to understand that the (variable a) is different than the array with the same name? If so then I understand.

a=a[..200] 'resize a[] to 200 elements "need clarification here"
The instructions on this page also say:
a=a[..50] 'extract first 50 elements of a[]
Am I to understand that a[..some number] could be either an extraction or a resize? If so please explain.


fredborg(Posted 2005) [#2]
Basically a[..count] means return a resized copy of array 'a' with 'count' elements. You could consider it to be the same as 'Dim a(count)' in other variants of Basic, although slices are much more flexible.

a=a[..] is kind of redundant, as it simply copies the original array 'a' back into 'a'. A more logical example would be:
b[] = a[..] which copies the contents of array 'a' into a new array 'b'.


bradford6(Posted 2005) [#3]

Local a[] = [11,22,33,44,55]
Print "a[] = [11,22,33,44,55]  creates the array on the fly"

Print "a[1] = "+a[1]
Print "a is "+Len(a)+" elements long"
a = a[..20]
Print "a[1] still = "+a[1]

Print "but Now, a is "+Len(a)+" elements long"




Floyd(Posted 2005) [#4]
The slice a[3..7] consists of elements of a starting at a[3] going up to, but not including, a[7].

It is a new array, built from the elements of a. An statement such as

a = a[3..7]

says to build this new subarray, then assign it to a.

It may help to consider ordinary variables:

Local x%
Local y#

This says x is an integer and y is a float. After they are declared they may be used as x,y without the % or #.

Local a%[]

This says the variable a is really an array. So the [ is rather like the % or # in declaring what kind of variable a is. In this case, a is an array of integers.
Local a%[] = [11,12,13,14,15]
Local b[]

b = a    ' b is now the same array as a

Print a[1]
Print b[1]
print

a = a[2..8]  ' pads with zeroes

For n = EachIn a
	Print n
next

As you can see from the a=b statement an array variable can be pointed at another array. But note that they are consistant; both are one dimensional arrays of integers.

This is how a=a[2..8] works. First the slice, a new array, is constructed. Then a is pointed at this new array.


PowerPC603(Posted 2005) [#5]
So, if you do this:
Local a%[] = [11,12,13,14,15]
Local Count% = 10

a = a[..Count]


Then BMax reads the original array (which is an object), creates a new empty array (new object of type Array) with size "Count" (= 0 <-> Count-1), copy all existing indexes from the original array to this new array, and put the pointer to this new array inside "a".
So that "a" points to the new array-object, while the original still exists in memory, but has no more reference to it.
At the next FlushMem, the original array will be deleted from memory.

Is all this correct?
If so, then I'm beginning to understand objects, I think.

Then this would be correct too:
Local a%[] = [11,12,13,14,15]
Local b[]
Local Count% = 10

b = a[..Count]


BMax creates an array object and puts it's pointer inside "a".
That array object would hold all values stated on the first line.
It also creates an empty array object and puts it's pointer inside "b".

Then on the line "b = a[..Count]", BMax would create again a new array object with size Count (= 0 <-> Count-1), copies all indexes from array "a" to this newly created array and puts the pointer of this new array object inside b, overwriting the pointer to the empty array object.

So after that line, "a" will still hold the pointer to it's array object that was created at the first line, "b" will hold the pointer to the newly created array object (created by using slices) and the empty array object (which was declared at the second line) is no longer referenced (but still exists in memory) and can be removed from memory with FlushMem.

Is this also correct?