Jump to content




How to make and use a look-up table


25 replies to this topic

#21 xgaarocha

  • Members
  • 18 posts

Posted 13 March 2013 - 01:19 PM

View PostTheOriginalBIT, on 12 March 2013 - 02:35 PM, said:

Even languages that have switches there are still advantages to using lookup tables. Think of dynamic content. You can use a look-up table, but you cannot use a switch.

Yeah, but for simple things, like checking input, switches are better in my opinion :)

#22 AgentE382

  • Members
  • 119 posts

Posted 09 August 2013 - 06:19 PM

I'm going to be a pain here, but I saw this in the "Ask a Pro Renewal Project" and just had to find this thread to fix it.

View PostSora the Hedgehog, on 17 February 2013 - 08:28 PM, said:

<snip>

function myFunction()
  print("Hello!");
end

function myOtherFunction()
  print("Goodbye!");
end

<snip>

local myLookupTable = {
  ["hi"] = myFunction,
  ["bye"] = myOtherFunction,
};

<snip>

if myLookupTable[input] then
  myLookupTable[input]();
else
   -- Just in case the user types a word that we don't know about.
   print("Word not found");
end

<snip>

write("Type a word : ");
local input = read();

if myLookupTable[input] then
  myLookupTable[input]();
else
  print("Word not found");
end

<snip>


[rant]
You don't need any of those semicolons. Lua is being nice to you and tolerating them. (In Lua 5.1, they can optionally follow a statement. They cannot define empty statements by themselves, like in C.) But using them after each statement in Lua is completely redundant, unlike using them in C.

Their only useful purpose is as a table field separator. In other words, use them exactly like, or instead of ",".

Also, their behavior changes between Lua 5.1 and Lua 5.2, just to give you a heads-up. I don't use them because I normally write for 5.2, but CC uses 5.1, so I need to keep my stuff compatible with both (and I don't like typing them).
[/rant]

------ Serious part ------

Using them that way is legal and you can use one after every statement if you want, but please refrain from doing so in beginner tutorials. That was very confusing for me as a beginner, having previous programming experience.

#23 Brekkjern

  • Members
  • 13 posts

Posted 09 August 2013 - 08:02 PM

How would you pass a function with arguments from such a table? Would that even work?

#24 AgentE382

  • Members
  • 119 posts

Posted 09 August 2013 - 09:39 PM

View PostBrekkjern, on 09 August 2013 - 08:02 PM, said:

How would you pass a function with arguments from such a table? Would that even work?

It depends. What exactly are you asking?

If you're asking "Can you pass arguments to functions stored in tables like that?", then the answer is "Yes."

This code:
function hello(name)	-- This function takes a single argument
	print("Hello, " .. name .. "!")	-- and uses the argument in its body.
end
function goodbye(name)	-- Same here.
	print("Goodbye, " .. name .. ".")
end
local lookupTable = {
	[1] = hello,
	[2] = goodbye
}
is equivalent to this code:
local lookupTable = {
	[1] = function(name)	-- Exactly the same as above, except we are directly storing the function in a table
		print("Hello, " .. name .. "!")	-- instead of storing it in a variable then putting the variable in the table.
	end,
	[2] = function(name)
		print("Goodbye, " .. name .. ".")
	end
}
You would use either like this:
lookupTable[1]("Your Name")	-- Call functions stored in tables exactly the same as a function normally stored in a variable.
lookupTable[2]("Your Name")

You could also do this:
local hello = lookupTable[1]	-- Take the function stored in the table and put it into a variable.
local goodbye = lookupTable[2]

hello("Your Name")	-- Then call the variable like normal.
goodbye("Your Name")

Functions are first-class values in Lua. In other words, whatever you can do to another type of value, you can (theoretically) do to a function. (I won't get into how to make that happen. It won't work unless you set it up yourself.)

So, you can store functions in a table and call them with or without any arguments, just like you would store them in a variable (the function name is actually a variable) and call them normally.

I hope that makes some sense. I'm kinda' tired right now. ;-)

#25 Brekkjern

  • Members
  • 13 posts

Posted 09 August 2013 - 09:49 PM

On further thought, I figured out how myself. I tried doing too much with just one table.

#26 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 10 August 2013 - 02:09 AM

Not sure if anyone pointed this out, but this table:

local validSides = {
  ["left"] = true,
  ["right"] = true,
  ["up"] = true,
  ["down"] = true,
}

Can be rewritten as:
local validSides = {
  left = true,
  right = true,
  up = true,
  down = true,
}

And the script would still work, as in, validSides["left"] and validSides.left would be the same thing.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users