This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Lettuce's profile picture

How to compare three values?

Started by Lettuce, 14 September 2012 - 12:06 AM
Lettuce #1
Posted 14 September 2012 - 02:06 AM
I'm surprised no one asked this before. How do I compare three values at the same time? In this case, to find the greatest number. Can this be shortened:
Spoiler

--dummy code
x1 = 1
x2 = 2
x3 = 3

if x1 > x2 then
   if x1 > x3 then
	  print "x1 is greatest."
   end
elseif x2 > x1 then
   if x2 > x3 then
	  print "x2 is greatest."
   end
end
elseif x1 == x2 or x2 == x3 or x1 == x3 then
   print "At least two values are the same.
end
else
print "x3 is greatest."
end
end

I pulled that out of my hat, so there may be errors, but you see what I'm trying to do. That will get ridiculously lengthy very quickly. And I was only trying to compare three numbers! Imagine if I had 20.

I've never been taught any programming language, save for HTML (and I know it doesn't count), so I learned most of what I know here at the forums/wiki. That's why I'm asking what is almost certainly a "noob" question.

–Lettuce
Lyqyd #2
Posted 14 September 2012 - 02:15 AM
Put all your numbers in a table.


local biggest = table[1]
for i=2, #table do
  if table[i] > biggest then
    biggest = table[i]
  end
end
MysticT #3
Posted 14 September 2012 - 02:16 AM
Well, to get the maximum/minimum you can use the math library/api:

local max = math.max(x1, x2, x3, x4, ...)
local min = math.min(x1, x2, x3, x4, ...)

Edit:
If you have all the values in a table, you can simply use:

local tbl = { x1, x2, x3, ... }
local max = math.max(unpack(tbl))
Lyqyd #4
Posted 14 September 2012 - 02:19 AM
Oh, durr. Don't know how I forgot about the min and max functions. I'd bet that math.max looks similar to that snippet I wrote above, though.
Lettuce #5
Posted 14 September 2012 - 02:20 AM
Okay. I kinda figured there would be an API. What's that unpack() though? I never used that, and it looks extremely useful. Could one compare two tables with that directly?
–Lettuce
Lyqyd #6
Posted 14 September 2012 - 02:32 AM
Unpack expands the values in a table into separate returns, so in this case, it causes all of the values of the table to become the arguments of math.max().
Lettuce #7
Posted 14 September 2012 - 02:38 AM
Interesting. Well, in a roundabout fashion, in my real program, I "unpacked" the hard way, counting occurrences of values in a table, so that will make my job easier. Thanks a lot guys!

–Lettuce
MysticT #8
Posted 14 September 2012 - 02:52 AM
Oh, durr. Don't know how I forgot about the min and max functions. I'd bet that math.max looks similar to that snippet I wrote above, though.
Yep, pretty much:

double m = args.checkdouble(1);
for ( int i=2,n=args.narg(); i<=n; ++i )
    m = Math.max(m,args.checkdouble(i));
return valueOf(m);
(It's the java code from LuaJ math library).
:)/>/>
Lyqyd #9
Posted 14 September 2012 - 03:02 AM
Oh, durr. Don't know how I forgot about the min and max functions. I'd bet that math.max looks similar to that snippet I wrote above, though.
Yep, pretty much:

double m = args.checkdouble(1);
for ( int i=2,n=args.narg(); i<=n; ++i )
	m = Math.max(m,args.checkdouble(i));
return valueOf(m);
(It's the java code from LuaJ math library).
:)/>/>

Haha, yeah, that's exactly it, for loop setup and all. That's a little nuts.