Page 1 of 1

Can't determine afterwards the first/last array-element

Posted: Fri Dec 27, 2013 1:08 pm
by joop1943
Hi,

I have a problem with determining afterwards the number of the first element and the number of the last element in an array.
I made a little stack to test.
All works well if the number of the last element of the array is 7 or less.
Above the 7 it gives strange results, results like these below:
(In this example array[x] expands from the first element x=4 to the last element x=9)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Element number Element Value

4 8
The number of elements in the Array is: 1
The first element in the array has number: 4
The last element in the array has number: 4
5 10
The number of elements in the Array is: 2
The first element in the array has number: 4
The last element in the array has number: 5
6 12
The number of elements in the Array is: 3
The first element in the array has number: 4
The last element in the array has number: 6
7 14
The number of elements in the Array is: 4
The first element in the array has number: 4
The last element in the array has number: 7
8 16
The number of elements in the Array is: 5
The first element in the array has number: 8
The last element in the array has number: 7
9 18
The number of elements in the Array is: 6
The first element in the array has number: 8
The last element in the array has number: 7
----------------------------------------------------------------------------------------------------------------------------------------------------------------------

I made a test-stack with 2 buttons and 3 card fields.

The first 2 card fields are input fields and are named "ElementFromField" and "ElementToField" in succession to input the numbers of the first and the last array elements.

Than a button named "Fill Array" that fills the Array.
The script is:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
global gMyArray, gFirstElement, gLastElement

on mouseUp
put card field "ElementFromField" into x
put card field "ElementToField" into y
if x>0 and y >= x
then
put "Element number" & tab & "Element Value" & return & return into card field "theResultField"
put x into gFirstElement
put y into gLastElement
repeat with theElement = x to y
put 2 * theElement into gMyArray[theElement]
put theElement & tab & tab & tab & tab & gMyArray[theElement] & return after card field "theResultField"
put the number of lines in the keys of gMyArray into p
put "The number of elements in the Array is: " & p & return after field "theResultField" 
put the first line of the keys of gMyArray into q
put "The first element in the array has number: " & q & return after field "theResultField" 
put the last line of the keys of gMyArray into r
put "The last element in the array has number: " & r & return after field "theResultField" 
end repeat
else
put "wrong numbers!" into card field "theResultField"
end if
end mouseUp
----------------------------------------------------------------------------------------------------------------------------------------------------------------------

The third field displays the results as seen above in the first box between the lines -------- and ----------.

The second button is named Clear Array and it does just that.
The script is:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
global gMyArray

on mouseUp
delete variable gMyArray
put empty into card field "theResultField"
end mouseUp
----------------------------------------------------------------------------------------------------------------------------------------------------------------------

The problem is: I want to create in my app each time an one-dimensional array with a different number of elements (and with different values for the first and last element numbers) and then determine afterwards what the first and the last element numbers are.

Am I doing something wrong?
Can anyone help me.
Thanks in advance.

Joop Sleijster

Re: Can't determine afterwards the first/last array-element

Posted: Fri Dec 27, 2013 4:47 pm
by dunbarx
I have not really looked at your stuff, but do be aware that arrays are not ordered. In other words, you might assemble them in a certain way, but they may not be stored that way. Arrays in LC are purely associative; they are not internally sorted, and do not reflect the order they were created. With a small data sample, perhaps they sort of do, but with larger ones they will not.

They must be changed back into ordinary variables and sorted "in the clear". Perhaps this is your issue?

Craig Newman

Re: Can't determine afterwards the first/last array-element

Posted: Fri Dec 27, 2013 4:58 pm
by FourthWorld
From the Dictionary entry for the "keys" function:
Note: The order of the keys is not alphabetical or chronological; it is based on the internal order. To obtain an alphabetical list of keys, use the sort command:

put the keys of myArray into myVariable
sort lines of myVariable

Re: Can't determine afterwards the first/last array-element

Posted: Fri Dec 27, 2013 5:50 pm
by joop1943
Thank you both Craig and Richard very much for your responses.
It's very clear to me now.

Again thank you.

Joop Sleijster

Re: Can't determine afterwards the first/last array-element

Posted: Fri Dec 27, 2013 7:10 pm
by jacque
Since your array keys are numbers, you can use the "extents" function which will give you the info you want and you won't have to sort.

Re: Can't determine afterwards the first/last array-element

Posted: Fri Dec 27, 2013 10:46 pm
by joop1943
Works perfect Jacqueline
Thank you very much for the hint.

Joop Sleijster