Different Results

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

Different Results

Post by bjb007 » Thu Sep 25, 2008 7:13 am

I have an array (sHits) of 12 elements.
e.g. 2,2,2,2,2,3,3,3,3,3,3,3

I have varOne = 5 and VarTwo = 2

I want to know if the array has 5
elements which contain 2.

This code

Code: Select all

   put 0 into bOn
   repeat with i = 1 to 12
      if sHits[i] = Var2 then
         add 1 to bOn
      end if
   end repeat
correctly counts 5 occurrences of 2.

Then I want to set the backgroundColor of
the fields containing 2 to red with this

Code: Select all

   if bOn = Var1 then
      repeat with k = 1 to 12
         if sHits[k] = Var2 then
            set the backgroundColor of field ("fldS" & k) to red
         end if
      end repeat
   else
       etc...
Problem is that all twelve fields have their
backgroundColor set to red when only the five
containing 2 should be.

Any help appreciated.
Last edited by bjb007 on Fri Sep 26, 2008 1:26 am, edited 1 time in total.
Life is just a bowl of cherries.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Sep 25, 2008 8:53 am

Hi Bbj,

It looks like sHits = Var2 the moment you enter the repeat loop. Note that your repeat loop doesn't change the values of i and Var2, so this is either true or false for all fields.

You shouldn't check the value sHits to see whether a field contains 2! Somewhere in your script, you need a line:

Code: Select all

if field k is 2 then
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Post by SparkOut » Thu Sep 25, 2008 11:13 am

or that if you do actually need to check the array for the match rather than check each field number then you need to be testing for sHits[k] in this loop, not - or change it to repeat with i = 1 to 12, not k.

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

Different Results

Post by bjb007 » Fri Sep 26, 2008 1:30 am

There was a typo in the code.

When I changed the code to check the value
in the field I corrected it so that worked but didn't
try the corrected code using the array value.

Thanks for the help.
Life is just a bowl of cherries.

Post Reply