Thread Tools Display Modes
09-07-08, 02:39 PM   #1
kerrang
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2006
Posts: 109
Renaming elements in a table

It's more complex that I thought...

Example

I have a table which looks a bit like this

MYTAB = {'FRED1','FRED2','FRED3',...

I want to change all the keys from 'FRED...' to 'BARNEY...' and my first thought was

Code:
for key in pairs(MYTAB) do
  _,_,rest = strfind(key,"FRED(.*))
  if rest then
    MYTAB['BARNEY'..rest] = MYTAB[key]
    MYTAB[key] = nil
 end
end
That doesn't work tho - I assume because I'm removing 'keys' from the array which confuses the 'pairs(MYTAB)' to death - or so it seems - it only processes the first element of the array anyway...

Am I stuck having to make a new table from the old one in fact?

e.g.

Code:
temptab = {}
for key in pairs(MYTAB) do
  _,_,rest = strfind(key,"FRED(.*))
  if rest then
    temptab['BARNEY'..rest] = MYTAB[key]
  else
    temptab[key] = MYTAB[key]
  end
end
MYTAB=temptab

Last edited by kerrang : 09-07-08 at 02:52 PM.
  Reply With Quote
09-07-08, 03:06 PM   #2
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
FRED1, FRED2, FRED3 etc. are not keys they are values, to set them as keys they would need to be FRED1 = true, FRED2 = true, FRED3 = true etc.

You made the same mistake in your other post.
  Reply With Quote
09-07-08, 03:11 PM   #3
kerrang
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2006
Posts: 109
Originally Posted by Slakah View Post
FRED1, FRED2, FRED3 etc. are not keys they are values, to set them as keys they would need to be FRED1 = true, FRED2 = true, FRED3 = true etc.

You made the same mistake in your other post.
If we ever need a syntax checker we'll call you...

Any chance you could answer the actualy question or are your talents limited to nitpicking?
  Reply With Quote
09-07-08, 03:37 PM   #4
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
There's a general problem within your code. So let's do some basics:
You are mixing up integer and string table indices.

If you declare a table with
Code:
MYTAB = {'FRED1','FRED2','FRED3',...
(only values, without indices) then you automatically build a table with integer indices.
The result is this:
Code:
MYTAB = {
	[1] = 'FRED1',
	[2] = 'FRED2',
	[3] = 'FRED3',
	...
	}
Now let's see your code:
Code:
for key in pairs(MYTAB) do
	_,_,rest = strfind(key,"FRED(.*))
	...
end
It will never work.

The construction
Code:
for k,v in pairs(t) do <stuff> end
will iterate over all key–value pairs of table t. Mind the k,v part! With
the table constructor
Code:
MYTAB = {'FRED1','FRED2','FRED3',...
which is equal to
Code:
MYTAB = {
	[1] = 'FRED1',
	[2] = 'FRED2',
	[3] = 'FRED3',
	...
	}
and with the expression
Code:
for key in pairs(MYTAB) do
you will get the keys only - which are just integer numbers. So your code
Code:
strfind(key,"FRED(.*))
won't work as expected.

Furthermore you can't access the table value via
Code:
MYTAB['BARNEY'..rest]
This is because 'BARNEY'..rest is a string index. But you build your inital table
with integer indices only (because you didn't specify any indices ... only values)

The only way to access the table
Code:
MYTAB = {'FRED1','FRED2','FRED3',...
is via integer indices:
Code:
MYTAB[1]  --returns the string "FRED1"
In conclusion this means:

1. There are two possible table index types: integers and strings. Don't mix them up.
I kindly ask you to read the "Programming in Lua" handbook section about tables
(http://www.lua.org/pil/2.5.html). I'm almost sure this will clarify a lot.

2. If you use string as indices (keys), then it is not possible to "change" them. You
could only change the values the keys are pointing to. (the only way is to remove
the old indices and to create new ones)

Hope this is somehow helpful for you.

Last edited by Duugu : 09-07-08 at 03:41 PM.
  Reply With Quote
09-07-08, 04:39 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
So it seems that Slakah did attempt to answer your question.

Please be kind when people try to help you. Otherwise you may find them not so willing next time.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
09-07-08, 04:49 PM   #6
kerrang
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2006
Posts: 109
I typed in an EXAMPLE - I rather hoped people would be smart enough to read what I asked (how to rename the keys of a table) and not just syntax check something I typed in off-the-top-of-my-head...

Instead I have a people picking syntax errors in my example code - if I wanted it syntax-checked I'd have typed into into WOW!!!!!

I'm sorry but if you READ WHAT I ASKED IN THE FIRST PLACE instead of just staring at the code and going "hmmmm, there's an error there" you'd not have wasted your time AND mine...

I'll answer my own question...

You have to use the latter approach (temporary table) - you can't change 'keys' in a table (whether they are explicit values or implied indexes they're still 'keys') because your loop (using pairs or ipairs) will fail when it finds something 'missing'...
  Reply With Quote
09-07-08, 04:59 PM   #7
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
doh

I spend 20 minutes to write down all the stuff solely because you posted a wrong example?
  Reply With Quote
09-07-08, 05:10 PM   #8
kerrang
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2006
Posts: 109
Originally Posted by Duugu View Post
doh

I spend 20 minutes to write down all the stuff solely because you posted a wrong example?
The clue was there!! - when I said the loop 'only renamed one element of the table' - that implied that I'd written working code already otherwise I'd not know that it failed after 1 loop

Copying the 50-60 lines of code from my addon didn't seem wise hence I created an example - my use of 'flippant' variable names was intended to show it was just an example and not 'proper' code

Great explanation of tables tho - I'm sure people will find it useful and it will Cut-and-Paste into other threads
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Renaming elements in a table


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