How to work with a broken range of numbers?

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
deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

How to work with a broken range of numbers?

Post by deeverd » Wed Oct 24, 2007 3:48 pm

Hello out there,

I'm trying to make a simple randomizer program for a high school classroom. What it is supposed to do is simply tell each of 8 groups of students which group goes first, then second, then third, all the way to the eighth group.

I know this has to be a very simple thing to do but the portion of the code I can't figure out is how to deal with a range of broken numbers. I'll post a part of the code below and then explain what I mean by broken numbers:


Code: Select all

 put random (8) into tNumberPick
  put word tNumberPick of field "numberField" into tChoice8
  
  repeat
    if tNumberPick = tChoice1 or tChoice2 or tChoice3 or tChoice4 or tChoice5 or tChoice6 or tChoice7 then
      put random (8) into tNumberPick
    else
      put tNumberPick into field "eighthField"
      exit repeat
    end if
  end repeat
   
  put tNumberPick into tChoice8
  wait 60 ticks
  

I'm doing random(8) because I need it to pick from 1 to 8 for the group numbers. What I mean by using a "broken range" is that the program needs to know what numbers have already been placed in each of the local variables.

I know the above code doesn't work using "tChoice1 or tChoce2" etc., so I'm assuming that "or" is not the correct expression. So my real question is how do I tell it to look for more than one number at a time when those numbers aren't in a normal low-to-high range? In other words, how would I tell it to be able to recognize "1 or 3 or 5 or 8" all at the same time in an if-then-else expression?

Thanks to whoever can tell me how to accomplish this task.
Cheers, deeverd

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

Post by Klaus » Wed Oct 24, 2007 5:57 pm

Hi deeverd,

If I understand you right you need a list of random numbers from 1 to 8?

Like:
2
5
4
3
1
8
7
6

Correct?

In that case you can:

...

Code: Select all

## generate a list from 1 to 8
repeat with i = 1 to 8
  put i & CR after newlist
end repeat
## get rid of trailing CR
delete char -1 of newlist

## Here the interesting part:
sort lines of newlist by random(10000)
answer newlist 
# or whatever...
...
Hope that helps.


Best

Klaus

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

There's Definitely Genius in Simplicity

Post by deeverd » Wed Oct 24, 2007 9:15 pm

Thanks Klaus,

There's definitely genius in simplicity. Your method is a lot faster and easier than the code I was producing.

While I am definitely going to use your method for this little program, I'm also still curious how one would write a line of script correctly that would say something like:

if tVariable = 1 or 4 or 7 then
do thisAction
end if

I know that "or" doesn't work in a situation like I was trying to use it, but what would one use? The reason I'm asking is that there have been other times that I wanted to write a simple line of code, having more than one item or number looked at all at the same time, but haven't found a way to accomplish that yet.

Thanks again. All the best, deeverd

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

Post by Klaus » Thu Oct 25, 2007 8:20 am

Hi deeverd,

you could use an "item" list like:

Code: Select all

...
if tVariable is among the items of "1,4,7" then
  do thisorthat
end if
...

Best

Klaus

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Now I Understand

Post by deeverd » Thu Oct 25, 2007 3:04 pm

Hello Klaus,

Once again, thank you very much. I now understand that I need to use the "is among" script and commas. No doubt I'll need that in the future.

Have a great weekend, deeverd

Post Reply