Select Case variable
case value0
code
case value1
code
etc.
case else
code
in .NET is used to do things based on a variable instead of using lots of if structures. Is there anything like this in Lua?
[Lua] [Question] Is there something like Select Case?
#1
Posted 27 August 2012 - 01:01 PM
#2
Posted 27 August 2012 - 04:02 PM
If I can help you, I will. But even if I can't, a bit more description means someone else might.
#3
Posted 27 August 2012 - 04:07 PM
Select Case daynumber 'we are testing the variable "daynumber"
case 0 'is it zero?
dayname = "Monday" 'do this if so
case 1 'or is it one?
dayname = "Tuesday" 'then do this
case 2 'and so on
dayname = "Wednesday"
case 3
dayname = "Thursday"
case 4
dayname = "Friday"
case 5, 6 'is it five OR six?
dayname = "Weekend!" 'then do this
case else 'did all of the above fail?
print("Error: invalid number.") 'then do these
dayname = "What?"
end select 'stop testing the variable
is equivalent to this (lua):
if daynumber == 0 then
dayname = "Monday"
elseif daynumber == 1 then
dayname = "Tuesday"
elseif daynumber == 2 then
dayname = "Wednesday"
elseif daynumber == 3 then
dayname = "Thursday"
elseif daynumber == 4 then
dayname = "Friday"
elseif daynumber == 5 or daynumber == 6 then
dayname = "Weekend!"
else
print("Error: invalid number.")
dayname = "What?"
end
so it's mostly organization.
#4
Posted 27 August 2012 - 05:52 PM
That looks a bit cleaner.
I should really use tables more often, they are quite useful. For an in-depth guide on them, check the ComputerCraft wiki.
Hope that helps.
-- Lettuce
#5
Posted 27 August 2012 - 06:02 PM
#6
Posted 27 August 2012 - 06:16 PM
#7
Posted 21 September 2013 - 10:27 AM
Lettuce, on 27 August 2012 - 06:16 PM, said:
10 Lines!
name = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
while true do
print "What day is it?"
daynumber = tonumber(io.read())
if daynumber < 1 or daynumber > 7 then
print "Where do you live?!"
else
print "It is" .. name[daynumber]
end
end
But this is not the same as a SELECT block.
#8
Posted 21 September 2013 - 10:35 AM
#9
Posted 21 September 2013 - 10:41 AM
name = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
while true do
print "What day is it?"
daynumber = tonumber(io.read())
if daynumber < 1 or daynumber > 7 then
print "Where do you live?!"
else
print "It is" .. name[daynumber] --# this line would error.
end
end
Dropping parentheses only works if you have a single, solitary literal string.
#10
Posted 21 September 2013 - 10:37 PM
name = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
while true do
print "What day is it?"
day = name[tonumber(io.read())]
print(day and "It is "..day or "Where do you live?!")
end
#11
Posted 22 September 2013 - 02:27 AM
BigTwisty, on 21 September 2013 - 10:37 PM, said:
name = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
while true do
print "What day is it?"
day = name[tonumber(io.read())]
print(day and "It is "..day or "Where do you live?!")
end
Lol. Nice try but you have to set the day variable, so it won't ever say "Where do you live?!". Nice try tho.
#12
Posted 22 September 2013 - 07:09 AM
AutoLocK, on 22 September 2013 - 02:27 AM, said:
BigTwisty, on 21 September 2013 - 10:37 PM, said:
name = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
while true do
print "What day is it?"
day = name[tonumber(io.read())]
print(day and "It is "..day or "Where do you live?!")
end
Lol. Nice try but you have to set the day variable, so it won't ever say "Where do you live?!". Nice try tho.
I do! On line 4, day is either set to the string for the current day or nil if the day number is out of bounds. Try it, it will work.
#13
Posted 22 September 2013 - 08:27 AM
worthless rewrite removed
-- snip --
Also based on that article I linked:
Considering how hard that is, I think "if then elseif else end" is far simpler, however this probably has its merits too. I am willing to bet one of the pros could easily mod the above to be far more efficient and reusable (generic) than it is now.
Edited by kreezxil, 22 September 2013 - 08:49 AM.
#14
Posted 22 September 2013 - 08:34 AM
name = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
while true do
print "What day is it?"
print(name[tonumber(io.read())] and "It is "..day or "Where do you live?!")
end
It seems the day variable in this version is never initialized.
By the way I think using a table to replicate switch / select is good for simple use cases but it miss break / continue statements and also doesn't let cascade program flow to the next case when not calling break.
#15
Posted 22 September 2013 - 08:48 AM
Shefla, on 22 September 2013 - 08:34 AM, said:
name = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
while true do
print "What day is it?"
print(name[tonumber(io.read())] and "It is "..day or "Where do you live?!")
end
It seems the day variable in this version is never initialized.
Your're right, I it's total garbage and can't be done the way I wrote it. Day must be captured as a variable before being processed inside the print string.
#16
Posted 22 September 2013 - 10:30 AM
kreezxil, on 22 September 2013 - 08:48 AM, said:
Shefla, on 22 September 2013 - 08:34 AM, said:
name = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
while true do
print "What day is it?"
print(name[tonumber(io.read())] and "It is "..day or "Where do you live?!")
end
It seems the day variable in this version is never initialized.
Your're right, I it's total garbage and can't be done the way I wrote it. Day must be captured as a variable before being processed inside the print string.
Nice try! This approach would work if you didn't have to print "It is " before the day.
name = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
while true do
print("What day is it? ", name[tonumber(io.read())] or "Where do you live?!")
end
#17
Posted 22 September 2013 - 12:22 PM
name = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
function getDay() day=name[tonumber(io.read())] if day ~=nil then print ("It is " .. day) end return day end
while true do print ("What day is it? (1-7) ",getDay() or "Where do you live?!") end
#18
Posted 22 September 2013 - 01:21 PM
#19
Posted 22 September 2013 - 05:40 PM
kreezxil, on 22 September 2013 - 12:22 PM, said:
name = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
function getDay() day=name[tonumber(io.read())] if day ~=nil then print ("It is " .. day) end return day end
while true do print ("What day is it? (1-7) ",getDay() or "Where do you live?!") end
Now you're just cheating. You're just stringing statements together. If you do that, you might as well just put it all on 1 line. Your code is actually quite a step back with respect to efficiency.
The point was to reduce the number of function calls as far as possible to show how little Lua actually needs select case statements. I think we've proven our point.
(I still think my coded was the smallest, though...)
#20
Posted 22 September 2013 - 08:54 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











