Page 1 of 1
repeat loop oddity
Posted: Sat Dec 14, 2013 6:07 pm
by billbedford
I have a button that imports data from a database. After passing the data through 'ConvertSQLCursorToArray' I'm left with an array with entries numbered from 1 to 211.
When I try and extract the entries with a repeat loop I get the following for the first iteration:
Code: Select all
repeat for each key tkey in theOrderArray
put theOrderArray[tkey][itemID] into theItem
tKey --> 35
Can anyone point me to why the array seems to be truncated?
Re: repeat loop oddity
Posted: Sat Dec 14, 2013 6:17 pm
by Klaus
Hi Bill,
no idea really without looking at the data/scripts!
Did you check if the number of keys is really 211?
If not then the error must be in your "ConvertSQLCursorToArray" script.
Or maybe the data in the database contain a numtochar(0) character?
That might cause an abrupt end of data.
Best
Klaus
Re: repeat loop oddity
Posted: Sun Dec 15, 2013 4:30 pm
by [-hh]
I once simply forgot to sort the keys. In case you also forgot it:
Code: Select all
put empty into sortedOut
repeat for each key tkey in theOrderArray
put CR & tkey && theOrderArray[tkey] after sortedOut
end repeat
delete char 1 of sortedOut -- is CR
sort sortedOut numeric by word 1 of each
Re: repeat loop oddity
Posted: Sun Dec 15, 2013 9:45 pm
by jacque
As -hh points out, the keys of an array are associative, not numerical, and once data is placed into an array it will be in an arbitrary order. It's likely that 36 is, in fact, the first key in the list.
The example -hh gives works. I generally do the sorting first, just to keep me sane, but either way is fine. I do it this way:
Code: Select all
put the keys of tArray into tKeys
sort tKeys numeric
repeat for each line l in tKeys
-- do stuff
end repeat
Re: repeat loop oddity
Posted: Sun Dec 15, 2013 9:57 pm
by Klaus
Ooops oh, well, looks like I completely misunderstood the problem!

Re: repeat loop oddity
Posted: Mon Dec 16, 2013 6:03 am
by jacque
It's okay Klaus. You still get credit for the other 5,000 correct answers you've given here.

Re: repeat loop oddity
Posted: Mon Dec 16, 2013 10:51 pm
by [-hh]
@ jacque and Klaus
Let me add one remark to your method to sort first. I tried to do this also following an earlier note of you.
But then I used *combine* to output the array.
Sad to say, combine respects the sort, but distorts the sortType and one has (in most cases) to sort again!
Example
Code: Select all
on mouseUp
repeat with j = 20 down to 1
put 100*j into a[21-j]
end repeat
put the keys of a into tKeys
sort tKeys numeric
combine a using return and comma
put a into fld "out"
end mouseUp
## Thereafter a is sorted, but as ascii, NOT numeric:
1,2000
10,1100
11,1000
12,900
13,800
14,700
15,600
16,500
17,400
18,300
19,200
2,1900
20,100
3,1800
4,1700
5,1600
6,1500
7,1400
8,1300
9,1200
By the way, as you are both grandmasters of LC.
jaques wrote: ... the other 5,000 correct answers you've given here ...
Has Klaus already given 82 incorrect answers here, did he?