Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Lua and Case Statement

Lua and Case Statement

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Gore   (207 posts)  Bio
Date Wed 04 Apr 2007 06:53 AM (UTC)
Message
I know Lua does not have a case statement, so I tried looking up something that would be similar and I found this site: http://lua-users.org/wiki/SwitchStatement , and I tried looking at the examples but there's a few things I don't quite understand.

do
  local switch
  function sayit(letters)
    for _,v in ipairs{sayit} do
      switch = switch or {a = "aah", b = bee, c = "see", ..., z = "zee"}
      local s = switch[v] or "what?"
      print(s)
    end
  end
end
sayit{'h','e','l','l','o','?'}


What does the switch = switch or {a = "aah", b = bee, c = "see", ..., z = "zee"} line do? How does whether to do one, -or- the other?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Wed 04 Apr 2007 07:13 AM (UTC)
Message
Well, for starters, it's important to point out that isn't actually Lua code, it's pseudo-code that is very close to Lua. E.g., the "..." should presumably be replaced with the whole alphabet.


Now, what the line is doing is simply creating the table called "switch" if it doesn't already exist. To create the table, it creates an index where the key is mapped to the thing to do. But this isn't actually doing anything, other than creating the index table. The next line, with switch[v], is where the table lookup is happening.

In this case, the "switch statement" (if one may call it that) is simply a mapping from string to string, saying what string to print for each input string.

Another option would be to map variable values to functions, but that's a little more complicated...

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Wed 04 Apr 2007 07:23 AM (UTC)
Message
Check out this post:

http://www.gammon.com.au/forum/bbshowpost.php?id=6219

It discusses the various ways you can do that in some depth.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Thu 05 Apr 2007 07:58 AM (UTC)
Message
Quote:

do
local switch
function sayit(letters)
for _,v in ipairs{sayit} do
switch = switch or {a = "aah", b = bee, c = "see", ..., z = "zee"}
local s = switch[v] or "what?"
print(s)
end
end
end


That is actually pretty obscure code. I don't see why it couldn't be written:


do
  local switch =  {a = "aah", b = bee, c = "see", ..., z = "zee"}
  function sayit(letters)
    for _,v in ipairs{sayit} do
      local s = switch[v] or "what?"
      print(s)
    end
  end
end


Even then it doesn't really seem to work as intended, as it is switching on "sayit" and not its argument. Plus, he has put "sayit" into { } brackets, which turns it into a table with one element in it. That won't work either. Also, "bee" isn't quoted. It should read something like:


do
  local switch =  {a = "aah", b = "bee", c = "see", z = "zee"}
  function sayit(letters)
    for _,v in ipairs (letters) do 
      local s = switch [v] or "what?"
      print(s)
    end
  end
end

sayit {'a', 'b', 'c'}


Unfortunately, as an example of Lua code, it is riddled with bugs.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Gore   (207 posts)  Bio
Date Reply #4 on Sat 07 Apr 2007 06:17 PM (UTC)
Message
local s = switch [v] or "what?"


What does that line signify? Why is the or in there?
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #5 on Sat 07 Apr 2007 06:37 PM (UTC)
Message
This is the line that actually does the switching. It scans through the switch variable defined above and will match the letter to the value. If not, a nil value is returned.

The or part is a bit odd, but standard Lua practice. When (foo or bar) is called and at least one of the values is non-boolean, the first value is always taken unless it is nil. If the first value is nil, then the second value is assigned. So in this example, if there is no valid data for the switch, a "what?" is used instead.

It is much easier to fight for one's ideals than to live up to them.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #6 on Sat 07 Apr 2007 07:13 PM (UTC)
Message
The values don't have to be non-Boolean. If you have a = false or true, a is true. If you get a = false or false then a is false. But yes, it's a very common idiom for setting default values in case the value provided was nil.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #7 on Sat 07 Apr 2007 07:26 PM (UTC)

Amended on Sat 07 Apr 2007 07:27 PM (UTC) by Shaun Biggs

Message
Well, if they are both boolean values, then the or is considered a boolean operator.

nil or false -> false
nil or true -> true
nil or 12 -> 12
12 or false -> 12
12 or true -> 12
false or 12 -> 12
true or 12 -> true
false or nil -> nil

So I guess if the first value is nil or false, then it's passed to the second value. Either way, in most languages you can't have an or in an assignment without the result being boolean.

It is much easier to fight for one's ideals than to live up to them.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #8 on Sat 07 Apr 2007 07:52 PM (UTC)
Message
Well, not really, it's just that most languages won't preserve the actual value. It's perfectly fine to do something like: var = thisIsAPointer || true; in which case it evaluates the whole as a boolean expression. But yeah, that'd be a little weird...

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #9 on Sat 07 Apr 2007 08:04 PM (UTC)
Message
You can do that, but it is considered horrific by programming standards. With Lua, the nil test is perfectly acceptable. This drives a few people nuts, but I love it.

It is much easier to fight for one's ideals than to live up to them.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #10 on Sat 07 Apr 2007 09:14 PM (UTC)
Message
Quote:

local s = switch [v] or "what?"

What does that line signify? Why is the or in there?


To put it another way ...

This is exactly equivalent to, but shorter than:


local s = switch [v] -- look up item in table

if s == nil then
  s = "what?"
end -- if


Basically it works because variables in Lua are untyped. That is, the value has a type, but the variable itself can hold any type.

If you take short-circuit boolean expression: this or that

Then the result is "this", but if "this" is considered false (that is, the value 'false' or 'nil' (without the quotes)), then the expression evaluates to "that" - whatever the type of "that" is - including nil.



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #11 on Sun 08 Apr 2007 01:13 AM (UTC)
Message
Quote:
You can do that, but it is considered horrific by programming standards
Well, that depends on who you ask. I think it obscures what is going on, but sometimes it is useful if you want to check if at least one pointer is non-null. Also, it can be useful if you want to check if one of several numbers is non-zero.

The thing to remember is that in C++ boolean expressions are boolean whereas in Lua there is the "this or that" situation Nick described.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


35,282 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.