Can't figure this one out!

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Can't figure this one out!

Post by bjb007 » Fri Oct 03, 2008 3:54 am

Another roulette prog I'm working on
needs some code and I can't figure out
how to make it work.

Loads a file of numbers - say 152.
Processes selected number - say 100.
A button to process next say 10 numbers -
so that stats can be calculated after each
ten additional numbers have been processed

Clicking the button for "Next 10" works fine
until the number of items left is < 10 + X
where X is probably 9.

Having processed 100 numbers and clicked
the "Next 10" button four times we've
processed 140 numbers. So numbers left
is 12 - which more that 10.

And this is where the problem lies.

Checking before processing 10 numbers
is "spinCount - processedCount > 10"

Of course as each number is processed
"processedCount" increases by 1 until
"spinCount - processedCount" is < 10"
so not all of these 10 numbers are process and
there's an error - even though there were
10 numbers available when the processing
started.

What I need is a way to process the loop
when there are 10 numbers available at
the start but not at the end of the loop.

The code I have doesn't handle this but
I'll include it anyway...

fileString contains the numbers from the loaded file.
"btnVar2" label is 10.

Code: Select all

if processedCount + the label of button "btnVar2" < spinCount then
      repeat with i = 1 to the label of button "btnVar2"
         send mouseUp to button ("btn"& item processedCount + i of fileString)
      end repeat
Life is just a bowl of cherries.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Fri Oct 03, 2008 8:33 am

Assuming you process the 10 (according to the button label) when there are 12 left, what happens to the 2 left over? Are you just ignoring the lines beyond that point? There's several ways I would do this, and probably have approached the whole thing in a different way to you, but trying to understand and help your situation, what is the nature of the "fileString" list? Does the value in the button label change over the processing procedure? What would be the case if you padded the end of the "fileString" with random or null lines to ensure that the number of lines you processed would be correct and ignore the odd 2 plus the padding? That might be a quick and dirty fix.

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Can't figure this one out!

Post by bjb007 » Fri Oct 03, 2008 9:15 am

SparkOut
I have another button to do the last numbers
one at a time or the value of btnVar2 could be
set the the appropriate value for number of
numbers left.

At the moment if I have btnVar2 set to five
and 50 numbers in a file I can load
say 30 to start then do them 5 at a time until
processedCount gets to 45 - then it can't do
the last 5. Not sure why as 50 - 45 = 5 still!

Really annoying...sitting here looking at those
two numbers and wondering why.

More sleep might help.
Life is just a bowl of cherries.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Fri Oct 03, 2008 9:34 am

bjb007 wrote:Not sure why as 50 - 45 = 5 still!
But that's slightly different to your code:

Code: Select all

if processedCount + the label of button "btnVar2" < spinCount then...
tests for: "if 45 plus 5 is less than 50 then..." so that's the reason for it stopping there as 50 is not less than 50. Presumably the processedCount increases by 1 each time it sends a button press to the variable button number, so testing for less than or equal "<=" would only get you one more iteration before it stops.

Does spinCount change over the process? Or is it set at the beginning to the number of lines of the fileString and remain constant?

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Can't figure this one out!

Post by bjb007 » Fri Oct 03, 2008 9:53 am

SparkOut

Yes, processedCount goes up for each button-press.
Tried putting it into a var at the start and checking
agains it but as it doesn't go up we're in an endless
loop.

Perhaps adding 1 to spinCount would do it - either
that or screw the whole thing up.
Life is just a bowl of cherries.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Fri Oct 03, 2008 10:12 am

Well, changing the spinCount (which I suggested through "padding" it with duff stuff) could be a solution. You could calculate how many items would be left over beforehand and add the right figures.

Code: Select all

send mouseUp to button ("btn"& item processedCount + i of fileString) 
if processedCount is being increased by one each time, as well as i being increased each time it loops, the button that's the target of the send mouseUp is definitely the right one? That is, (assuming that processedCount is initialised to zero) the first time through the loop, button number (item 0 plus 1) will be pressed, the next time it will be button number (item 1 plus 2) and so on.
So if your fileString was 26,31,18,46,9 then you'd be pressing button number 26, then button number 18, then button 9... (as both i and processedCount are adding one each time around the loop). Would that be right?

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Fri Oct 03, 2008 10:50 am

Anyway, this might be the sort of technique you need:

Code: Select all

   --Calculate the number of loops you can fit in

   put min (the label of button "btnVar2", spinCount - processedCount) into tLoop

   --so if the spinCount is 52 and the label is 10, then with a processedCount up to 40
   --tLoop will be set to 10, as spinCount minus processedCount will be at least 12.
   --When the processedCount reaches 50, spinCount minus processedCount will only be 2,
   --so tLoop will be set to 2 (as the minimum of the two options).



   repeat with i = 1 to tLoop
     send mouseUp to button ("btn"& item processedCount + 1 of fileString)
     -- Assuming processedCount starts at zero, I think you need to add ONE
     --to the processedCount, not i
     --otherwise you will be taking every second result in the fileString, not each in turn


   end repeat
I've put a test stack up on RevOnline for you to try. My user space, title bjb007 Looping

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Can't figure this one out!

Post by bjb007 » Fri Oct 03, 2008 1:59 pm

Thanks SparkOut.

Done too much Rev so will get some
sleep and tackle it on the morrow.
Life is just a bowl of cherries.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”