Page 1 of 1
Simple logic, but, is another way Possible?
Posted: Mon Aug 13, 2012 4:28 am
by iwannaprogram
Hey guys, I am determining whether a variable has either 2, 3 or 4.
In this example , localValueN has "2"
Code: Select all
local localValueN
put "2" into localValueN
if localValueN = "2"
then
put "is 2" into anotherVar
else
if localValueN = "3"
then
put "is 3" into anotherVar
else
put "is 4"into anotherVar
end if
end if
answer anotherVar
Question 1:
Can anyone think of a leaner, smarter, or shorter way to write this logic?
Question 2:
Can we put more code into each line? maybe some break that LiveCode understands when code starts or ends??
like this
Code: Select all
local localValueN ; put "2" into localValueN ; if localValueN = "2" ; then; put "is 2" into anotherVar ; else ; if localValueN = "3" ; then ; put "is 3" into anotherVar ; else ; put "is 4"into anotherVar ; end if ;end if
answer anotherVar
Re: Simple logic, but, is another way Possible?
Posted: Mon Aug 13, 2012 6:53 am
by shaosean
You can use the semi-colon as a line break in Rev, so your second example will work, but I think it is still too wordy, so try this:
Code: Select all
local localValueN
local anotherVar
put ("is" && localValueN) into anotherVar
answer anotherVar
Re: Simple logic, but, is another way Possible?
Posted: Mon Aug 13, 2012 7:25 am
by dave_probertGA6e24
Or you could use a Switch statement to cover multiple choices:
Code: Select all
switch localValueN
case "2"
// do something when it is 2
break
case "3"
// likewise for value = 3
break
case "10000000000"
// 'cos it's a nice big number (which I really wish I had in my bank account!)
break
default
// Handle all other options that are not covered above
end switch
I personally use switch's for any multi value variable (with a simple number of results) instead of a bunch of if's - mainly because they are more readable to me.
As with most languages though - there are a few ways of achieving the results you want - dependent on the effect you are after.
Cheers,
Dave
Re: Simple logic, but, is another way Possible?
Posted: Mon Aug 13, 2012 9:52 am
by Klaus
Hi,
this is everything but OFF-TOPIC!?
Will move the thread...
Re: Simple logic, but, is another way Possible?
Posted: Mon Aug 13, 2012 1:52 pm
by sturgis
You can also do it this way.
put any item of "2,3,4" into localValueN
Code: Select all
if localValueN = "2" then put 2 into anotherVar -- No need for end if as long as its all on 1 line
if localValueN = "3" then put 3 into anotherVar
if localValueN = "2" then put 3 into anotherVar
I kinda prefer switch too though.
For something like this you could also do a simple substitution list
Code: Select all
put "1,2,3,4,5,6,7,8" into tNums -- define matching lists
put "a,b,c,d,e,f,g,h" into tLetters
put any item of tNums into n
get itemoffset(n,tNums) -- find what item number n is
put item it of tLetters into anotherVar -- anotherVar now contains the matching item from tLetters
Of course if you're going to do that, and have an exact match value to value you can put everything into an array and lookup based on the array key.
Re: Simple logic, but, is another way Possible?
Posted: Mon Aug 13, 2012 7:45 pm
by Paul.RaulersonBUSIvAg
I am very new and I just know I am showing my ignorance, but could you not just say something like this?
Code: Select all
if ((localValueN >1) and (localValueN < 5)) then
put "is " & localValueN into anotherVar
end if
That is assuming of course, you are only interested in values of 2, 3, or 4. Might be some kind of "in" construct in the language.
Like:
Code: Select all
if localValueN is in range 2 to 4 then put "is " & localValueN into anotherVar
I have not tested that to be sure it is legal LC syntax.
Yours,
-Paul
iwannaprogram wrote:Hey guys, I am determining whether a variable has either 2, 3 or 4.
In this example , localValueN has "2"
Code: Select all
local localValueN
put "2" into localValueN
if localValueN = "2"
then
put "is 2" into anotherVar
else
if localValueN = "3"
then
put "is 3" into anotherVar
else
put "is 4"into anotherVar
end if
end if
answer anotherVar
Question 1:
Can anyone think of a leaner, smarter, or shorter way to write this logic?
Question 2:
Can we put more code into each line? maybe some break that LiveCode understands when code starts or ends??
like this
Code: Select all
local localValueN ; put "2" into localValueN ; if localValueN = "2" ; then; put "is 2" into anotherVar ; else ; if localValueN = "3" ; then ; put "is 3" into anotherVar ; else ; put "is 4"into anotherVar ; end if ;end if
answer anotherVar
Re: Simple logic, but, is another way Possible?
Posted: Mon Aug 13, 2012 8:05 pm
by mwieder
Your first example is fine. For the second, try
Code: Select all
if localValueN is in "234" then put "is " & localValueN into anotherVar
I normally prefer the switch construct as well, but there are many ways of getting a solution to any given problem.