repeat loop oddity

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
billbedford
Posts: 1
Joined: Mon Nov 18, 2013 10:53 pm

repeat loop oddity

Post by billbedford » Sat Dec 14, 2013 6:07 pm

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?
Bill Bedford

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: repeat loop oddity

Post by Klaus » Sat Dec 14, 2013 6:17 pm

Hi Bill,

no idea really without looking at the data/scripts! 8)

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: repeat loop oddity

Post by [-hh] » Sun Dec 15, 2013 4:30 pm

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
shiftLock happens

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: repeat loop oddity

Post by jacque » Sun Dec 15, 2013 9:45 pm

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
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: repeat loop oddity

Post by Klaus » Sun Dec 15, 2013 9:57 pm

Ooops oh, well, looks like I completely misunderstood the problem! 8)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: repeat loop oddity

Post by jacque » Mon Dec 16, 2013 6:03 am

It's okay Klaus. You still get credit for the other 5,000 correct answers you've given here. :)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: repeat loop oddity

Post by [-hh] » Mon Dec 16, 2013 10:51 pm

@ 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?
shiftLock happens

Post Reply