Smaller than/bigger than

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Coder
Posts: 8
Joined: Fri Apr 02, 2010 6:27 pm

Smaller than/bigger than

Post by Coder » Mon Jul 12, 2010 9:46 am

What's the script for smaller than and bigger than? I have tried "<" and ">" but doesn't work.. :roll:

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Smaller than/bigger than

Post by jmburnod » Mon Jul 12, 2010 10:26 am

Hi Coder

< and > works only with integer

If you post your script, i see it

Regards

Jean-Marc
https://alternatic.ch

Coder
Posts: 8
Joined: Fri Apr 02, 2010 6:27 pm

Re: Smaller than/bigger than

Post by Coder » Mon Jul 12, 2010 10:32 am

My task is (school):

"You are required to write a program which calculates the amount of pay per hour based on the age and experience of an employee.
If an employee is aged less than 18 years old, they are paid £5 per hour. If they are aged between 18 and 21 they are paid £6 per hour and if they are aged over 21 they are paid £8 per hour.
In addition, if an employee has worked for the company for more than 3 years, they are given a 15% pay rise, and if they are a supervisor they are given a 20% pay rise."

Hope you got a clearer picture :oops:

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Smaller than/bigger than

Post by dunbarx » Mon Jul 12, 2010 2:34 pm

You are probably making some simple error. The following should give you beeps:

if 5 > 3 and 5 < 8 then beep 3

Note that these operators can also compare characters, based on their ASCII values, so:

if "a" < "c" then beep 3

Coder
Posts: 8
Joined: Fri Apr 02, 2010 6:27 pm

Re: Smaller than/bigger than

Post by Coder » Tue Jul 13, 2010 9:26 am

Yes, but we need to write a program with something like 2 boxes.

Where you enter the age, and then press a button and the 2nd Box shows you how much money the person would get.

So my script started like:

on mouseup
if field testfield1 is < 18 then put 5 into field testfield2
end mouseup

But there is still an error :(

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Smaller than/bigger than

Post by Regulae » Tue Jul 13, 2010 11:13 am

The problem might be in the precise structure of your script.
on mouseup
if field testfield1 is < 18 then put 5 into field testfield2
end mouseup
... won’t work as written, but:

Code: Select all

on mouseUp
   if field testfield1 < 18 then
      put 5 into field testfield2
   end if
end mouseUp
... does work, assuming you have loaded the names of the fields into the variables testfield1 and testfield2. Say if they were called “First” and “Second” you would need:

Code: Select all

put "First" into testfield1
put "Second" into testfield2
... either in the mouseUp, or somewhere else in your stack, i.e. the variables must be defined. Possibly the cause of your problem is:

Code: Select all

if field testfield1 < 18 then
... note this does not use “is” as in your example “if field testfield1 is < 18 then”. Rev is so “English like” it is easy to add in an “is” that doesn’t work with “<” in the programming language. Also, you need to “close off” an “if” statement with an “end if”. You may also want to check that field testfield1 contains a number and alert the user if not. Here’s the script I tested:

Code: Select all

on mouseUp
   put "First" into testfield1
   put "Second" into testfield2
   put empty into field testfield2
   if field testfield1 is not a number then
      beep 1
      answer "Please enter a number into the first field"
      pass mouseUp
   end if
   if field testfield1 < 18 then
      put 5 into field testfield2
   end if
end mouseUp
... I also found it convenient to clear field testfield2 each time, so I could test it repeatedly.
I hope there’s something here of help.

Regards,

Michael

Coder
Posts: 8
Joined: Fri Apr 02, 2010 6:27 pm

Re: Smaller than/bigger than

Post by Coder » Tue Jul 13, 2010 11:58 am

Thanks!

Yes it helped me, but dont understand several things.

[*]Sometimes I don't need to use "end if" at the end and it works, why?
[*]When you put the fields into variables..

Code: Select all

put "First" into testfield1
put "Second" into testfield2
I would do that like this:

Code: Select all

put field testfield1 into First
put field testfield2 into Second
Is that wrong?
[*]But then, in your last Script, you didn't use the variables at all. Because you still used testfield 1& 2 instead of First & Second

Thank you again :wink:

€: E.g. this works as well

Code: Select all

on mouseUp
if field testfield1 < 18 then put 5 into field testfield2
end mouseUp
/without "end if"

I'm a bit confused now :D

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Smaller than/bigger than

Post by dunbarx » Tue Jul 13, 2010 2:13 pm

You need to learn a little more about the basic syntax of the language. Check out the "if" control structure in the dictionary, and see the several different forms it can take. Be careful about just writing what you think sounds grammatically correct, but does not follow the language rules.

You can say: if 4 > 3 then beep 3

But you cannot say: if 4 is > 3 then beep 3.

There just isn't anything in the language that allows that; "is" does not appear at all in the use of comparative operators. Read the dictionary and look at the examples.

So however english-like Rev is, you still have strict rules on how to code. Play with short simple scripts that do little things. Try five different ways to write an "if" construction. Then try a "repeat" construction. You will get it very quickly.

Craig Newman

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Smaller than/bigger than

Post by Regulae » Tue Jul 13, 2010 4:52 pm

Hi Coder,

On the question of “if ... then” you are quite right- you don’t have to “close off” with “end if”. In the Rev menubar under “Help”, “Dictionary” I looked up “if” and there are four forms of the “if” control structure. If you want to execute just one statement in the “if”, e.g.

Code: Select all

on mouseUp
   if  field "Test" = 3 then beep 1
end mouseUp
... then, assuming you have a field “Test” and have typed “3” into it, pressing a button which contains the above script will produce a beep, as you expect. If you want several statements to be executed in the “if”, you can use:

Code: Select all

on mouseUp
   if field "Test" = 3 then 
      beep 1
      answer "Hello Coder!"
      beep 1
   end if
end mouseUp
... again, assuming a field “Test” you’ve typed “3” into, pressing the button will produce a beep, a message “Hello Coder!”, then when you press “OK”, another beep. So you use “end if” for multiple statements. I must admit I’ve always used the “end if” form as a matter of habit from using other languages, and also because of the ease of adding more statements into the conditional as a project develops. I’m glad you asked the question because it made me read the “if” section of the Rev dictionary again and realised I had an “end if” habit. It’s good to know the alternatives.

On the question about my use of variables and field names, perhaps the way I wrote the example was confusing. I had constructed my test script to be “self contained”- assuming you had a field whose name is “First” (i.e. field “First”) and a field whose name is “Second” (i.e. field “Second”) and a button with the script:

Code: Select all

on mouseUp
   put "First" into testfield1
   put "Second" into testfield2
   put empty into field testfield2
   if field testfield1 is not a number then
      beep 1
      answer "Please enter a number into the first field"
      pass mouseUp
   end if
   if field testfield1 < 18 then
      put 5 into field testfield2
   end if
end mouseUp
... everything would work for you straight away. This makes the example work and uses your original variables testfield1 and testfield2, but we could do things more simply by writing:

Code: Select all

on mouseUp
   put empty into field "Second"
   if field "First" is not a number then
      beep 1
      answer "Please enter a number into the first field"
      pass mouseUp
   end if
   if field "First" < 18 then
      put 5 into field "Second"
   end if
end mouseUp
... so you are quite right that in my original example I seemed to do something unnecessary- putting the word “First” into the variable testfield1 and the word “Second” into the variable testfield2. It’s important to be clear on what is a variable, like testfield1, and what it contains, like “First”. From you last post:
When you put the fields into variables..

Code: Select all

put "First" into testfield1
put "Second" into testfield2
I would do that like this:

Code: Select all

put field testfield1 into First
put field testfield2 into Second
There is an important difference between these statements:

Code: Select all

put "First" into testfield1
... puts the word “First” into the variable testfield1. If we have a field “First” on our card, we can now refer to it as field testfield1. An example of how we might use this in a button script:

Code: Select all

global testfield1, testfield2

on mouseEnter
   put "First" into testfield1
   put "Second" into testfield2
end mouseEnter

on mouseUp
   put empty into field testfield2
   if field testfield1 is not a number then
      beep 1
      answer "Please enter a number into the first field"
      pass mouseUp
   end if
   if field testfield1 < 18 then
      put 5 into field testfield2
   end if
end mouseUp
... if we “load” the names of the fields into the variables testfield1 and testfield2, we can easily modify the script to refer to two different fields “Third” and “Fourth”, simply by changing the mouseEnter:

Code: Select all

on mouseEnter
   put "Third" into testfield1
   put "Fourth" into testfield2
end mouseEnter
... which can be very convenient.
On the other hand:

Code: Select all

put field testfield1 into First
... means something quite different. This is an instruction for Rev to place whatever the user has typed into field testfield1 into the variable First. Actually, as written, this won't work, and at this point I have a confession to make- my choice of names “First”, “Second”, “Third”, “Fourth” is not a good idea and the reason is interesting. As names for objects like fields/buttons these words will work- but not as variables. That is, you can’t say:

Code: Select all

put 10 into first
...Rev’s script editor won’t compile it and for a very good reason. “First”, “second”, “third”... are keywords used to refer to particular objects, e.g. a button with the script:

Code: Select all

on mouseUp
   go to the first card of this stack
end mouseUp
... will take you to the first card of the stack. An acceptable alternative would be:

Code: Select all

put field testfield1 into firstValue
put field testfield2 into secondValue
... firstValue and secondValue are variable names that will work perfectly well. When these instructions are executed, whatever the user has typed into field testfield1 will be placed in the variable firstValue and whatever the user has typed in field testfield2 will be placed in secondValue. You can then do things with these variables, e.g.

Code: Select all

on mouseUp
   put field testfield1 into firstValue
   put field testfield2 into secondValue
   if firstValue < secondValue then
      put firstValue * secondValue into field testfield3
      beep 1
      wait 1 second
      beep 1
   end if
end mouseUp
... this isn’t a very interesting example but it gives the idea. Notice that because we are multiplying firstValue by secondValue we need both to be numbers- if the user typed “George” into field testfield1 and “5” into field testfield2 they would get an error message when they pressed the button. “George” * 5 doesn’t make any sense. To recap:

Code: Select all

put "MyFirstField" into testfield1
... puts the word “MyFirstField” into the variable testfield1. If later in the script we refer to field testfield1, it will mean field “MyFirstField”. If we then said:

Code: Select all

put field testfield1 into firstValue
... the variable firstValue will now contain whatever is typed into field testfield1, which we have already said means field “MyFirstField”. testfield1 is a variable in which we store the name of the field, in this case “MyFirstField”. If you have on the card a field “MyFirstField” and a button with the following script:

Code: Select all

on mouseUp
   put "MyFirstField" into testfield1
   put field testfield1 into firstValue
   answer firstValue
end mouseUp
... type something into field “MyFirstField” and press the button, Rev will display a message dialog that uses whatever you typed. Again, this is not very exciting but does illustrate the use of variables. I hope some of these examples help. Your questions are perfectly sensible, and there were aspects of what I wrote that were certainly confusing. Please keep firing as many questions to the forum as you need. I’ve found the folks on the forum incredibly helpful, and different people explain things in different ways which helps everyone. Indeed, after preparing this and returning to the forum, I see Craig is already on the case.

Regards,

Michael

Post Reply