iPremiumZ
|
  |
| Joined: 23 Jan 2012 |
| Total Posts: 6834 |
|
|
| 13 May 2012 06:57 PM |
Yes, that might seem like a misleading title. But I was wondering if this is how you use dictionaries?
local Dictionary = { ["iPremiumZ"] = "coolkat" ["Noob"] = "Noob" ["random guy"] = "random" }
I was also wondering, how would you print something from that, would it be the same as a table;
print(Dictionary[2])
-- iPremiumZ, Programmer, Forumer, Game Designer |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:02 PM |
I really dislike dictionaries too, lol. But they can end up being useful.
Dictionaries actually work like this.
local Dictionary = { ["iPremiumZ"] = "coolkat"; ["Noob"] = "Noob"; ["random guy"] = "random" }
--Side note, you forgot the commas/semicolons
print(["iPremiumZ"])
> coolkat
To be completely honest, I've never really used dictionaries..They didn't really serve a real purpose for me. For me, it's easier to create a table within a table for things like this. |
|
|
| Report Abuse |
|
|
iPremiumZ
|
  |
| Joined: 23 Jan 2012 |
| Total Posts: 6834 |
|
|
| 13 May 2012 07:03 PM |
Yeah @ dark.
This isn't for a game or anything.
I don't really think I'll use them, but I am trying to learn more about tables and I figure they're part of them, so why not?
-- iPremiumZ, Programmer, Forumer, Game Designer |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:04 PM |
Got sidetracked in that post, lol.
So basically, you can't access the object by numbers anymore with a dictionary. You have to access it through what you named it.
So in this code..
local Dictionary = { ["iPremiumZ"] = "coolkat"; ["Noob"] = "Noob"; ["random guy"] = "random" }
print(Dictionary[2]) print(Dictionary["Noob"])
You would get
> nil > Noob |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:06 PM |
Aye, doing things like this will help you learn a lot faster. That's similar how I learned how to use tables. Technically you don't need the quotes though:
Dictionary = { Apple = "An apple!"; Ohio = "Where crazyman32 lives"; LOL = "Laught out loud"; }
print(Dictionary["Apple"]) > An apple! |
|
|
| Report Abuse |
|
|
iPremiumZ
|
  |
| Joined: 23 Jan 2012 |
| Total Posts: 6834 |
|
|
| 13 May 2012 07:06 PM |
Thanks all!
-- iPremiumZ, Programmer, Forumer, Game Designer |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:08 PM |
You can embed functions too!
Dictionary = { Test = function() print("Hello from Dictionary table!") end; }
Dictionary.Test() |
|
|
| Report Abuse |
|
|
iPremiumZ
|
  |
| Joined: 23 Jan 2012 |
| Total Posts: 6834 |
|
|
| 13 May 2012 07:10 PM |
woah
So like this would work?
Dictionary = { msg = function() mess = Instance.new("Message", Workspace) mess.Text = "omg omg i lrnd dictionrys111" end; }
Dictionary.msg() -- iPremiumZ, Programmer, Forumer, Game Designer |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:13 PM |
Yes, indeed it will! This is why Lua can also be called Object-Oriented-Programming, since you can use tables to make pseudo-classes.
To make a function a method (which allows access to itself), you call it with : instead:
MyClass = { Name = "Crazyman32"; Points = 1337;
GetName = function(self) return self.Name end; GetPoints = function(self) return self.Points end; }
print(MyClass:GetName()) > Crazyman32 |
|
|
| Report Abuse |
|
|
iPremiumZ
|
  |
| Joined: 23 Jan 2012 |
| Total Posts: 6834 |
|
|
| 13 May 2012 07:16 PM |
So dictionaries are pretty helpful.
This should be cool.
One more question, could I do this;
Class = { Weapon = Workspace.Part; }
-- iPremiumZ, Programmer, Forumer, Game Designer |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:21 PM |
Yup.
Anything inside that table can be as if you are using normal variables. You can even add tables WITHIN tables:
Class = { OtherStuff = { Test = "LOL"; } }
print(Class.OtherStuff.Test) > LOL
Then you can get into metatables and stuff. That's when stuff gets really fun since you basically create the environment of how the table works :P |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:22 PM |
@Crazy; That first method you posted is the method I use in my admin commands actually.
Dictionaries can make life a ton easier. Well, maybe not life, but it can make coding a lot easier.
Also, a cool somewhat related thing is a dictionary inside a table. Check this out.
local tab = { {name = "bob", awesome = true, power = 10000}, {name = "john", awesome = false, power = -999} }
for i, v in pairs(tab) do print(v.name) if v.awesome then print(v.name.." is awesome!") else print(v.name.." is a loser. :c") end print("Vegeta, what's the scouter say about his power level?") if v.power > 9000 then print("IT'S OVER 9000!") else print("Lame.") end end
Tables are really fun to screw around with actually.. |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:23 PM |
Muahaha, how do you like your late toast?
But yeah, tables are fun. They are part of the reason why messing around with Lua is a lot of fun. |
|
|
| Report Abuse |
|
|
iPremiumZ
|
  |
| Joined: 23 Jan 2012 |
| Total Posts: 6834 |
|
|
| 13 May 2012 07:25 PM |
That's pretty lejit
-- iPremiumZ, Programmer, Forumer, Game Designer |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:25 PM |
Oh noes, my toast is burnt now!
You can do some really weird things with tables. Tableception! |
|
|
| Report Abuse |
|
|
| |
|
iPremiumZ
|
  |
| Joined: 23 Jan 2012 |
| Total Posts: 6834 |
|
|
| 13 May 2012 07:31 PM |
This isn't about dictionaries, but tables.
Is this how you'd pick a random number from a table
array = [1,2,3,4,5,6,7,8,9,10]
print(array[math.random(1,#array))]
-- iPremiumZ, Programmer, Forumer, Game Designer |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:35 PM |
| Yes, but it would be {}, not []. |
|
|
| Report Abuse |
|
|
|
| 13 May 2012 07:59 PM |
Actually, you don't need the first '1' argument. If you just put in a number, it will assume you want a number between 1 and the inputted argument:
local t = {"A","B","C","D","E"}
local Random = t[math.random(#t)] -- Between index 1 and 5 |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 13 May 2012 08:14 PM |
"array = [1,2,3,4,5,6,7,8,9,10]
print(array[math.random(1,#array))] "
Python code :D
from random import randint array = [1,2,3,4,5,6,7,8,9,10] print(array[randint(1,len(array))]) |
|
|
| Report Abuse |
|
|