Jump to content




[Lua] [Question] Is there something like Select Case?


  • You cannot reply to this topic
20 replies to this topic

#1 ThinkInvisible

  • Members
  • 24 posts

Posted 27 August 2012 - 01:01 PM

The code
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?

#2 Lettuce

  • Members
  • 210 posts
  • LocationIn your fridge.

Posted 27 August 2012 - 04:02 PM

I'm... sorry. I don't understand your question. I've never worked with .NET, so I'm unsure what you are going for here. Could you tell me what sort of program you are writing? I'm assuming you mean something like conditional statements, but I (or probably anyone, for that matter) don't know what you need without more detail.
If I can help you, I will. But even if I can't, a bit more description means someone else might.

#3 ThinkInvisible

  • Members
  • 24 posts

Posted 27 August 2012 - 04:07 PM

This (VB.Net):
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 Lettuce

  • Members
  • 210 posts
  • LocationIn your fridge.

Posted 27 August 2012 - 05:52 PM

After running several trials to see if I could shorten that, the best I found was to try a table:
Spoiler

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 ardera

  • Members
  • 503 posts
  • LocationGermany

Posted 27 August 2012 - 06:02 PM

I know what you mean, in JAVA there is something like this too, but lua hasn't this feature... I think its added in 5.2 but Im not sure... (CC uses 5.1)

#6 Lettuce

  • Members
  • 210 posts
  • LocationIn your fridge.

Posted 27 August 2012 - 06:16 PM

Found an even better way. Still using tables, shortened last post to 11 lines. Previously it was 21.
Spoiler
That's as clean as you're gonna get it in Lua.

#7 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 21 September 2013 - 10:27 AM

View PostLettuce, on 27 August 2012 - 06:16 PM, said:

Found an even better way. Still using tables, shortened last post to 11 lines. Previously it was 21.
Spoiler
That's as clean as you're gonna get it in Lua.

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 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 21 September 2013 - 10:35 AM

Sweet holy Jesus found this great article on how to implement SELECT CASE into lua and from what I know I can do with LUA in CC 1.53, this can be done. http://dev.oz-apps.com/?p=265

#9 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 21 September 2013 - 10:41 AM

To answer the OP's question, no, there isn't. Either use elseifs, or find another, more efficient way to implement your solution.



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 BigTwisty

  • Members
  • 106 posts

Posted 21 September 2013 - 10:37 PM

6 lines!
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 Dave-ee Jones

  • Members
  • 456 posts
  • LocationVan Diemen's Land

Posted 22 September 2013 - 02:27 AM

View PostBigTwisty, on 21 September 2013 - 10:37 PM, said:

6 lines!
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 BigTwisty

  • Members
  • 106 posts

Posted 22 September 2013 - 07:09 AM

View PostAutoLocK, on 22 September 2013 - 02:27 AM, said:

View PostBigTwisty, on 21 September 2013 - 10:37 PM, said:

6 lines!
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 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 22 September 2013 - 08:27 AM

-- snip --
worthless rewrite removed
-- snip --

Also based on that article I linked:
Spoiler

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 Shefla

  • Members
  • 30 posts

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 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 22 September 2013 - 08:48 AM

View PostShefla, 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 BigTwisty

  • Members
  • 106 posts

Posted 22 September 2013 - 10:30 AM

View Postkreezxil, on 22 September 2013 - 08:48 AM, said:

View PostShefla, 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 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 22 September 2013 - 12:22 PM

What about this at 3 lines!
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 ElvishJerricco

  • Members
  • 803 posts

Posted 22 September 2013 - 01:21 PM

So I think the underlying point everyone's trying to make here is the same one the creators of Lua believe in: Lua doesn't need select case (or switch case, as it is called in most languages). Lookup tables, functions as values, and if/elseif things are more than enough to do it; possibly more compactly, but definitely in a more Lua-like style.

#19 BigTwisty

  • Members
  • 106 posts

Posted 22 September 2013 - 05:40 PM

View Postkreezxil, on 22 September 2013 - 12:22 PM, said:

What about this at 3 lines!
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 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 22 September 2013 - 08:54 PM

I don't see anywhere in this thread where readability was stated as a rule. But I'll give you the point. In fact I thought it was Select Case, did you have any comments on the article I found and my implementation of it?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users