10-10-05, 02:02 PM | #1 |
Do I understand this correctly?
Sorry but I've been caught by the programing bug, and I need to know if I understand this code correctly
Code:
---{AddRoll trigers each time roll window is opened. name and roll are extracted before this function, doesn't really matter how, they ar extracted}--- local function addRoll(name, roll) for i=1,40 Rolls[i] = roll ---{Creates a table called Rolls, with numbers from 1-40 as it's keys, and roll as it's values}--- for a=1,40 if Rolls[a] == nil then break ---{Reduces the size of the Rolls table to the exact number of rolls}--- for b=a,1,-1 if Rolls[b] == Rolls[a] then Rolls[a] = Rolls[a] + 0.001 ---{Checks for two equal rolls, and increase one slighty}--- if (rollTable[name]) then ---{If rollTable[name] exists then}--- rollTable[name] = roll; ---{set name as rollTable's keys and roll as it's values}--- return; ---{Hummm what does return do?}--- end rollTable[name] = roll; ----{creates a rollTable talbe with name as it's key.....}--- table.insert(nameList, name); ----{creates a nameList talbe with name as it's values}--- end local function rollCompare(i,j) return rollTable[i] < rollTable[j]; end local function rollSort() table.sort(namesTable, rollCompare); ---{Sorts using rollCompare function}--- end Cop_item_drop() for i,name in ipairs(nameList) do ---{no god damn idea}--- local roll = rollTable[name]; ---{no god damn idea}--- DEFAULT_CHAT_FRAME:AddMessage(name .. " had roll " .. roll); end |
|
10-11-05, 02:18 AM | #2 |
It's easy to understand tables if you grasp the following concept: all tables or arrays in LUA are key-value pairs. There are no other types of tables or arrays.
For example, the following command creates an empty table: Code:
local tableNames = { }; Code:
tableNames["NewKey"] = "NewKeyValue"; Code:
local indexTable = { }; Code:
table.insert( indexTable, "ValueForNumber1" ); Code:
local newTable = { }; newTable["NewKey"] = "NewValue"; table.insert( newTable, "ValueForNumber1" ); local count = table.getn( newTable ); Adding tables to tables is the most powerful feature possible. Here is an example of a phone book table. When you understand it, no tables will ever produce difficulties to you: Code:
-- This code will create a phonebook whose internal structure looks like this: -- PhoneBook -- | - Key = 1, Value = PhoneBookEntry -- | - Key = 1, Value = CustomerName -- | - Key = 2, Value = CustomerNumber -- | - Key = 2, Value = PhoneBookEntry -- | - Key = 1, Value = CustomerName -- | - Key = 2, Value = CustomerNumber local PhoneBook = { }; -- Create a new phone book entry local PhoneBookEntry = { }; -- Add customer name and number to the phone book table.insert( PhoneBookEntry, "John Doe" ); table.insert( PhoneBookEntry, "+45-666-666-666" ); -- Add it to the phone book table.insert( PhoneBook, PhoneBookEntry ); -- Let's create a new phone book entry, this time using direct-indexing. -- Using this method will result the entry having keys '1' and '2', -- with values "Jane Doe" and the phone number, respectively, -- but table.getn returns zero for this entry, because table.insert was not used. local PhoneBookEntry2 = { 1 = "Jane Doe", 2 = "+45-999-999-999" }; -- And add it to the book table.insert( PhoneBook, PhoneBookEntry2 ); |
|
10-11-05, 09:15 AM | #3 |
Thanks alot man. I'm going to take a good look at this and think about it before asking any dumb questions :P
|
|
10-11-05, 10:34 AM | #4 |
I'm going to try and explicit what this phone book function does for me
Code:
-- This code will create a phonebook whose internal structure looks like this: -- PhoneBook -- | - Key = 1, Value = PhoneBookEntry -- | - Key = 1, Value = CustomerName -- | - Key = 2, Value = CustomerNumber -- | - Key = 2, Value = PhoneBookEntry -- | - Key = 1, Value = CustomerName -- | - Key = 2, Value = CustomerNumber local PhoneBook = { }; -- Create a new phone book entry local PhoneBookEntry = { }; -- Add customer name and number to the phone book table.insert( PhoneBookEntry, "John Doe" ); table.insert( PhoneBookEntry, "+45-666-666-666" ); Code:
PhoneBookEntry[1] == "John Doe" PhoneBookEntry[2] == "+45-666-666-666" ---bloody satanist! :P Then you insert it's values into PhoneBook Code:
local PhoneBookEntry2 = { 1 = "Jane Doe", 2 = "+45-999-999-999" }; table.insert( PhoneBook, PhoneBookEntry ); Code:
PhoneBook[1] == PhoneBookEntry[] --- With PhoneBookEntry[] the entire PhoneBookEntry table Then you create a second table Code:
PhoneBookEntry2 = { 1 = "Jane Doe", 2 = "+45-999-999-999" }; Code:
table.insert( PhoneBook, PhoneBookEntry2 ); Code:
PhoneBook[2] == PhoneBookEntry[2] --- With PhoneBookEntry[] the entire PhoneBookEntry table http://www.imagedump.com/index.cgi?p..._id=0&warned=y ( I forgot one line but nevermind) One question. Is John Doe, considered a Key or a Value? Is it both? It's the key to the PhoneBookEntry[] table, but a value to the PhoneBook[] talbe. So which is it? |
|
10-11-05, 11:48 AM | #5 |
generally it's function(key, value)
so table.insert( key, value ); edit: nm. in this case it looks like it's table.insert ( index, value ); http://lua-users.org/wiki/TablesTutorial that should help |
|
10-11-05, 01:23 PM | #6 |
Yup I understood the basics of tables now :P I'm just wondering how lua treats they values of PhoneBook, since they are keys to another table... If I wanted to modify PhoneBook's values, I guess I would modify PhoneBookEntry's keys, rather than trying to do something like
PhoneBook[1] = "hello" Or would both work? Would PhoneBook[1]="hello" set PhoneBook's first value as hello and PhoneBookEntry's first key as "hello"?, and would PhoneBookEntry[1] = PhoneBookEntry["hello"] (that does raise the question, how does one change the key value of a table? I'll look around and see what I can find...) do the same job? |
|
10-12-05, 03:11 AM | #7 |
The PhoneBook table has indexed keys 1 and 2. The values associated with these two keys are tables themselves.
So, if still following the original design pattern,this piece of code would run through the phone book and list all customer names: Code:
for _, PhoneBookEntry in PhoneBook do print( PhoneBookEntry[1] ); end; The values in PhoneBook are not keys to another tables. They ARE tables. You must remember that when you add a specific value (whether it was a string, an integer or a table variable) to a table, LUA creates a copy of this into the table where you're inserting it. Unlike the reference manual states, the iteration loop will not give you references to actual values, but copies of them. For this reason, the following will not work: Code:
for _, PhoneBookEntry in PhoneBook do PhoneBookEntry[1] = "Jack Doe" -- Attempt to change every customer name to Jack Doe. Does not work. end; Code:
for key, _ in PhoneBook do PhoneBook[key][1] = "Jack Doe" -- 'key' indexes the first table, which are PhoneBookEntry tables. -- '1' indexes the PhoneBookEntry table, and in key '1' we placed the customer name. Code:
-- Create an empty PhoneBook local PhoneBook = { }; -- Create a new entry. The bracketed strings distinquish keys from values. local PhoneBookEntry = { ["Name"] = "John Doe", ["Number"] = "+45-666-666-666" }; -- Add it to the phone book table.insert( PhoneBook, PhoneBookEntry ); -- Iterate through the phone book for _, PhoneBookEntry in PhoneBook do print( PhoneBookEntry.Name ); -- The indexing system here is called "LUA shorthand" print( PhoneBookEntry.Number ); -- Again, indexes in shorthand. -- print( PhoneBookEntry["Name"] ); -- Non-shorthand (= full) indexing -- print( PhoneBookEntry["Number"] ); -- The same, non-shorthand indexing end; Code:
PhoneBook[1] = PhoneBookEntry Code:
PhoneBook.1 = PhoneBookEntry Hope this helps. - Gorak |
|
10-12-05, 07:23 AM | #8 |
Thanks alot man, that really helps :P Things are actually starting to make sens!
|
|
WoWInterface » Developer Discussions » Lua/XML Help » Do I understand this correctly? |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|