Thread Tools Display Modes
11-04-05, 10:28 AM   #1
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
I hate lua.

Code:
function GetGuilNumbers()
    numGuildMembers = GetNumGuildMembers();
	for i=1,numGuildMembers do
	name[i], _, _, _, _, _, _, _, _, _= GetGuildRosterInfo(i);
	end
end

function SetTicketNumberToNil()
	for k,v pairs do
		name[k]=name[v]
	end
end

Does the above set the name table's keys as it's values, and it's values as it's keys,making it look like this:
Code:
Keys        Values
name1       1
name2       2
name3       3
.......         ...
.......         ...
nameN       N
Or does it change it to the following
Code:

Keys        Values
name1     name1
name2     name2
name3     name3
.......         ...
.......         ...
nameN       nameN
  Reply With Quote
11-04-05, 12:26 PM   #2
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
Second question:

Code:
local name{}
function GetGuilNumbers()
    numGuildMembers = GetNumGuildMembers();
	for i=1,numGuildMembers do
	name[i], _, _, _, _, _, _, _, _, _= GetGuildRosterInfo(i);
	end
end

function SetTicketNumberToNil()
	for k,v pairs do
		name[k]=name[v]
		name[v]=0
	end
	for i=1,numguildMembers do
		table.insert(EasierFrameShowing[i],name[i])
end
Will I end up with a table which would look like this:

Code:
EasierFrameShowing
      |-Key=1 Value=Name 
                                      | Key = Name[1]  (for example shouryuu)
                                      | Value = Name[1] (should be 0)
Or something else?
  Reply With Quote
11-04-05, 03:01 PM   #3
Cladhaire
Salad!
 
Cladhaire's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Jul 2005
Posts: 1,935
The first question is.. what are you trying to accomplish.

The first example looks like you'd get the folllowing:

You have name, which you populate with numerically indexed names of the members in the raid. So if its you and I.. we'd be:

Code:
name = {[1] = "Cladhaire", [2] = "Shouryuu"]}
Then your code does something that I'm not understanding. You go through and set the table to the following:

Code:
name = {[1] = nil, [2] = nil};
Because name[v] doesn't have a value. if you're looking to make a double-reverse-lookup table, you can use:

Code:
function SetTicketNumberToNil()
	for k,v in name do
		name[v]=name[k]
	end
end
But I'm not sure exactly what you're looking for. My code would give you:

Code:
name = {
[1] = "Cladhaire",
[2] = "Shouryuu",
["Cladhaire"] = 1,
["Shouryuu"] = 2,
};

Last edited by Cladhaire : 11-04-05 at 03:05 PM.
  Reply With Quote
11-04-05, 03:08 PM   #4
Cladhaire
Salad!
 
Cladhaire's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Jul 2005
Posts: 1,935
Second Question

First step, you build the table indexed by raidnumber. All is well and good.

Then you go through and do the same thing as before, setting name[1] and name[2] to nil.
Then you go and set name["Cladhaire"] and name["Shouryuu"] to 0;

The last bit of code makes the following in EasierFrameShowing:

Code:
EasierFrameShowing = {[1] = nil, [2] = nil};
  Reply With Quote
11-04-05, 03:08 PM   #5
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
Hummm ponder ponder... I'll take some time to think about this and hit ya with another question if I fail :P
  Reply With Quote
11-04-05, 03:11 PM   #6
Cladhaire
Salad!
 
Cladhaire's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Jul 2005
Posts: 1,935
It more comes down to what you're trying to accomplish =).
  Reply With Quote
11-04-05, 09:40 PM   #7
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
It's simple enough I want to inverse a table's keys and values. I want the keys to become the values, and the values to become the keys.

Would something along these lines work?
Code:
function SetTicketNumberToNil()
	for k,v pairs do
		table.insert(name, name[i], i)
	end
end
This only work, if it does, because the keys are simple: 1,2,3,4,5... But how do I do that if I have keys like zdfs and gkfslg; or djakf?

Last edited by shouryuu : 11-04-05 at 09:43 PM.
  Reply With Quote
11-04-05, 11:07 PM   #8
Trimble Epic
An Aku'mai Servant
 
Trimble Epic's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 31
Lightbulb

here's how I would do what I think you're trying to do

Code:
function SetTicketNumberToNil(name)
	local new_name = {}  -- use a temporary table
	for k,v in name do -- iterate over every field in the table
		new_name[v]=k -- just stick 'em in the new table
	end
	table.setn(new_name,table.getn(name)) -- might not be needed, but you can do this to be thorough
	return new_name -- return the new table
end
  Reply With Quote
11-05-05, 05:37 AM   #9
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
ugh i feel so stupid... :P well thanks!

EDIT: I'm keeping the dumb questions flowing. Is the "for k,v" statement general? By that I mean does it work for every table, or is it an example everyone takes and everyone understands?

Last edited by shouryuu : 11-05-05 at 09:02 AM.
  Reply With Quote
11-05-05, 11:36 AM   #10
Trimble Epic
An Aku'mai Servant
 
Trimble Epic's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 31
the common notation 'for k,v in table do' is used because it's easy to read/recognize.

Whenever you use the 'generic' form of the for statement on a table WITHOUT an iterator (i.e. 'for k,v in table' vs. for 'k,v in pairs(table)' ), the for statement automatically provides 2 variables for each run through the loop - the key and it's associated value - thus k and v. When we (teachers?) give helpful examples, we commonly use k,v to emphisize the key and the value inside the sample.

yes, 'for k,v...' works on every table.
  Reply With Quote
11-05-05, 12:31 PM   #11
shouryuu
A Chromatic Dragonspawn
 
shouryuu's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 150
Cool, thanks a lot! i don't understand why tables are giving me so much trouble...
  Reply With Quote
11-05-05, 06:33 PM   #12
Trimble Epic
An Aku'mai Servant
 
Trimble Epic's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 31
My pleasure.

I really like the way tables work. They are remarkably versatile.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » I hate lua.


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