if NOT statements

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Locked
stonepimp
Posts: 5
Joined: Fri Sep 23, 2005 3:34 am

if NOT statements

Post by stonepimp »

Hi folks,

love the work you guys are doing and so am building a few lua games... I'll release them when I'm comfortable at others laughing at my code ;-) or more likely when they work ok.

anyway, rather than a huge mess of complicated recursive code, is it possible to have a statement that looks like the following code

if variable1 ~= variable2 ~= variable3 ~= variable4 then
-- do something
end

also I've checked the math. wiki descriptions but couldn't find anything. Is there anyway to compute factorials ? ie 4! (4*3*2*1) - other than a self written recursive function (again!).

many thanks guys
SP.
BePe86
Posts: 10
Joined: Sun Oct 02, 2005 3:36 am
Location: Norway

Post by BePe86 »

Hi. you could do if var1 ~= var2 and var2 ~= var3 and var3~=4 ... ... then
...
end...

but that looks ugly, and using tables with for is better.

As for the factorial, you could also use for here as well, but I prefer the recursive thing.
EU PSP 1.52 -> 2.0 -> 1.50
Untold Legends
32MB + 1GB Memory Sticks
stonepimp
Posts: 5
Joined: Fri Sep 23, 2005 3:34 am

Post by stonepimp »

yeap tried to go down that route, but...

variable1 = "10"
variable2 = "20"
variable3 = "30"
variable4 = "10"

so using "if var1 ~= var2 and var2 ~= var3 and var3~=4"

the above statement would be true (var1 doesn't equal var2, variable2 doesn't equal var3 and var3 doesn't equal var4) but...

I want var1 ~=var2 ~= var3 ~= var4 (ie none of these variables can be the same...

Please note the solution for the above is simple but when you have 10+ variables the number of combinations grows significantly !

(eg. 3628800 combinations for 10 variables - and I'm not written that and statement out by hand!).

Any other ideas? will try anything you suggest !?%@

;-)
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

Store all variables in a table, use two nested for loops, where you compare in the inner loop the indices of both loops and if they are not equal, you compare the values. If they are equal, return false, otherwise return true at the end of the function.

But this is not Lua Player related, I just don't want this to be a general Lua help forum, take a look at http://www.lua.org/ http://lua-users.org/wiki/TutorialDirectory http://www.lua.org/lua-l.html and the very good book http://www.lua.org/pil/
Locked