results to a test i don't understand

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
magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

results to a test i don't understand

Post by magice » Tue Mar 24, 2009 10:53 pm

I have finished my first app, but the scripting is messy with lots of repetitive put commands and set commands. So, i am trying to learn the proper use of the repeat command and how to call variable values from within the repeat command in order to make my coding a bit more elegant. in order to learn how it works, i wrote a series of test scripts for evaluating the results. In one of the tests I got results that made no sense to me and i was hoping that someone could shed some light on them for me. Here is my test:

First i created 20 text boxes and named them with numbers 1-20
second, I entered the value of their name into the text boxes (entered 1 in the text box named 1, 2 into the text box named 2 etc.)

then i scripted this code into a button

Code: Select all

on mouseUp
   local tVar
   
   repeat with x = 1 to 20
      do "put field "  &x&  " into tVar"
      put tVar into myArray[(100 + the value of x)]
   end repeat
   
   combine myArray using tab
   answer myArray
end mouseUp
I expected to get an answer back of 1 2 3 4 etc. all the way to 20
instead, the numbers were jumbled in an order of 1, 6 ,11,16,17,12,7,2,18,13,8,3,19,14,9,4,20,15,10,5

i fixed this problem by adding the word field at the beginning of each fields name ( now field2 field2 field3 etc) and adding an extra field into the do command. the code now looked like this

Code: Select all

on mouseUp
   local tVar
   
   repeat with x = 1to 20
      do "put field field"  &x&  " into tVar"
      put tVar into myArray[(100 + the value of x)]
   end repeat
   
   combine myArray using tab
   answer myArray
end mouseUp
my results were as i had expected on the first test, 1-20 in numerical order.

what i don't understand is why the code didn't work the first time. I suspect that it might have something to do with the order in which the boxes were created. Which, due to copying and pasting, is not the same order in which they were named.

Any light to shed?

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

Post by Mark » Wed Mar 25, 2009 12:44 am

Hi magice,

You can't do this. If you call fields 1, 2, 3 etcetera, Revolution will interprete the names as field numbers rather than names. It is safest to start field names with a non-numerical character. It is even better to give fields descriptive names such as "age", "phone number" and "first name".

You can still use a repeat loop. If all fields have descriptive names, you can use a list of names:

Code: Select all

repeat for each item myField in "age,phone number,first name"
  put myField into fld myField -- display field name in field
end repeat
If you have a lot of fields, you could give them names such as "F01", "F02", "F03" et cetera. Then create the following repeat loop:

Code: Select all

repeat with x = 1 to 20
  set the numberFormat to "00"
  put "F" & x*1 into myField
  put the short name of fld myField into fld myField
end repeat
You could also group those 20 fields and make a repeat loop that reads and writes to each of the fields:

Code: Select all

repeat with = 1 to number of fields of grp "Your Group"
  put the short name of fld x of grp "Your Group" into fld x of grp "Your Group"
end repeat
I wouldn't start a field name with "field" because this might be confusing for the engine, too. It makes your code less readable as well.

Best regards,

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

Post Reply