Thread Tools Display Modes
10-10-05, 02:02 PM   #1
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
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
Sorry if I'm asking things explained in the LUA docs, but I just don't understand them... The only programs I ever wrote where dumb programs for my calculator, ends there... I;m having trouble with tables sob sob sob
  Reply With Quote
10-11-05, 02:18 AM   #2
Gorak
A Fallenroot Satyr
Join Date: Oct 2005
Posts: 21
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 = { };
The following will add a new key called "NewKey" to the table, and set this key's value companion to a value "NewKeyValue". If the key already exists, the old one is replaced:
Code:
tableNames["NewKey"] = "NewKeyValue";
These above examples deal with a so-called reference-keyed table. The functions table.insert, table.remove and so on deal with an index-referenced table. Again, this would create an empty table:
Code:
local indexTable = { };
The following would add a new value into the table, using the first key available. In this case, since the table is empty, this key will be number 1:
Code:
table.insert( indexTable, "ValueForNumber1" );
When table.insert is used, it always checks the highest numerical index in the table, and adds the new value to the next available index. It also increases the table's internal variable 'n' which dictates the number of entries in the table. If you use the direct indexing method to add a key-value pair, the 'n' is NOT increased, thus the following code:
Code:
local newTable = { };
newTable["NewKey"] = "NewValue";
table.insert( newTable, "ValueForNumber1" );
local count = table.getn( newTable );
The resulting 'count' will be 1, because direct indexing does not increase the internal value. The table itself DOES contain both key "NewKey" with value "NewValue" and key 1 with "ValueForNumber1". But table.getn only sees the addition made with table.insert.

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 );
I hope this simplifies and clarifies things for you. Tables are a very powerful feature when used and understood correctly.
  Reply With Quote
10-11-05, 09:15 AM   #3
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
Thanks alot man. I'm going to take a good look at this and think about it before asking any dumb questions :P
  Reply With Quote
10-11-05, 10:34 AM   #4
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
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" );
After this we have the following table:
Code:
PhoneBookEntry[1] == "John Doe"
PhoneBookEntry[2] == "+45-666-666-666"    ---bloody satanist! :P
You have a table called PhoneBookEntry, who's first key is one, and first value is John Doe, and who's second key is 2, and second value +45 666 666 666, right?

Then you insert it's values into PhoneBook
Code:
local PhoneBookEntry2 = { 1 = "Jane Doe", 2 = "+45-999-999-999" };

table.insert( PhoneBook, PhoneBookEntry );
So we should have the following
Code:
PhoneBook[1] == PhoneBookEntry[] --- With PhoneBookEntry[] the entire PhoneBookEntry table
Correct?

Then you create a second table
Code:
PhoneBookEntry2 = { 1 = "Jane Doe", 2 = "+45-999-999-999" };
And insert it in the PhoneBook table
Code:
table.insert( PhoneBook, PhoneBookEntry2 );
So you end up with this
Code:
PhoneBook[2] == PhoneBookEntry[2] --- With PhoneBookEntry[] the entire PhoneBookEntry table
So if we were to visualize PhoneBook[] it would look like this:
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?
  Reply With Quote
10-11-05, 11:48 AM   #5
Hwap
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 7
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
  Reply With Quote
10-11-05, 01:23 PM   #6
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
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?
  Reply With Quote
10-12-05, 03:11 AM   #7
Gorak
A Fallenroot Satyr
Join Date: Oct 2005
Posts: 21
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;
As you can see, the iteration loop gives us a key-value pair for each existing key in PhoneBook. The values are PhoneBookEntry tables. Since we added two entries to the phone book, the result of this run are two printed lines, first one reading "John Doe" and second "Jane Doe".

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;
Instead, the following code would do the trick:
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.
Furthermore, it is important to remember that the tables in LUA will only get those indexed keys if you tell it to do so. For example, if we were to change the keys of PhoneBookEntry to something more specific, the above code would no longer work, since the key '1' no longer exists. What you place inside the brackets is the key, no matter if it's a string or a number. If it is a string, it must be surrounded by quotes. The following piece of code demonstrates this:
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;
The above code example shows that the keys used need not be numbers. They can be anything you like. The shorthand system will only work if the keys are string values, because anything following the dot in an indexing call must be a string. It cannot be a number. This is the reason why
Code:
PhoneBook[1] = PhoneBookEntry
works, but
Code:
PhoneBook.1 = PhoneBookEntry
results an error while compiled. The latter verson is interpreted as PhoneBook["1"], and as we recall, there are no keys in the table that would match this. Remember, in here the '1' is not an integer, but a string variable.

Hope this helps.

- Gorak
  Reply With Quote
10-12-05, 07:23 AM   #8
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
Thanks alot man, that really helps :P Things are actually starting to make sens!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Do I understand this correctly?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off