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
09-07-08, 05:23 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
kerrang, the reason why we ask to view your code is so that we can see what you have and why things aren't working for you. If you give an example snippet of code, and that example is not working code, then how do we know what is in your working copy? Dummy variables, sure. But we would expect to see the dummy variables surrounded by working code - if not, then we assume that how your example code looks, is how it looks in your addon.

Think of it this way: You bring your car into a mechanic and say "I was driving along yesterday, and I heard something like a 'kachink kachink' sound while the engine was running." The mechanic spends his time looking for what would have caused a 'kachink kachink' sound and doesn't find anything, so he takes it out for a test drive, and hears a different kind of sound instead, which implies a different sort of problem with your engine. When he brings that up to you, you say "The 'kachink kachink' sound was only an example - I didn't say that it really sounded like that."

When you say, this doesn't work for me - this is how I have it in my addon, someone will look at it and say, "well, there's your problem right there." Unless they're psychic, they won't know what your real code looks like.


edit: and for long snippets of code, you may use http://wowuidev.pastey.net/
__________________
"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, 05:29 PM   #10
kerrang
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2006
Posts: 109
If people read what I asked instead of just leaping-in and looking for faults in the code - there wouldn't have been any confusion...

The thread title and OP are asking how you rename keys/elements in a table - the OP basically says "do I have to create a new table?" (the answer to which is yes).

If people don't read what I said and just leap into the code looking for syntax errors - that's not really my fault - they might, in future, realise that not everyone who posts code is looking for something as simple as a syntax error

In the meanwhile - I'll just stop asking questions because it seems to upset people...
  Reply With Quote
09-07-08, 05:44 PM   #11
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
We did read your question.

MYTAB = {'FRED1','FRED2','FRED3',...
I want to change all the keys...
Code:
for key in pairs
That doesn't work tho
Both people gave you information on how to make your example work so that you did not need to create a new table.

You do not have to stop asking questions, but maybe read through your question before you post it, and ask yourself if you are giving people all of the information that they need in order to help you. People are not upset that you are asking questions, it's just that you did not give proper information and then insulted responses that you received, and faulted them for not seeing your "clue".
__________________
"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, 06:20 PM   #12
kerrang
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2006
Posts: 109
I'll end this by suggesting that you guys have never seen the exam paper which looks like this


EXAM- READ THE PAPER FULLY BEFORE STARTING...

Q1 What is your name


Q2 What is your favourite color


Q3 What class do you play in WOW


Q4 What is the worlds fastest land mammal


Q5 What is the worlds tallest building


Q6 What is your favourite race in WOW


Q7 What are baby Foxes called





Q8 Do not answer the other questions - just sign the exam and hand it in...

  Reply With Quote
09-07-08, 07:32 PM   #13
rodrick
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 34
Thank you to the people who continue to try and help others even after being insulted by some for trying to do so. You help make this community great.
  Reply With Quote
09-07-08, 08:59 PM   #14
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Yes, kerrang, I have seen that go around, though I have not had a sneaky devil of a professor that tried to pass that off to us before.
__________________
"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-08-08, 09:42 AM   #15
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Ok I've tested this and I cannot replicate the errors you are encountering.

Here's my code:

toc:
Code:
## Interface: 20300

## Title: Test
## Notes: Test


## SavedVariables: Test

Test.lua
lua:
Code:
local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:SetScript("OnEvent", function(f)
	Test = {
		FRED1 = "CAT",
		FRED10 = "MOO",
		FREDDOG = "DOG"
	}
	for k, v  in pairs(Test) do
		rest = string.match(k,"FRED(.*)")
		if rest then
			Test['BARNEY'..rest] = v
			Test[k] = nil
		end
	end
	for k, v in pairs(Test) do
		DEFAULT_CHAT_FRAME:AddMessage(k.." :  "..v)
	end
	f:UnregisterEvent("ADDON_LOADED")
	f:SetScript("OnEvent", nil)
end)
and finally it outputs:
Code:
BARNEY10 :  MOO
BARNEY1 :  CAT
BARNEYDOG :  DOG

So I have no clue what's going wrong.

Also as a quick note on my first reply, I am not a mind reader, if you come to me with a bucket with a hole in it and ask me why the water is slowly disappearing then I'll tell you there is a hole in your bucket. Your example code was wrong so I told you that was probably your issue.

Last edited by Slakah : 09-08-08 at 02:40 PM.
  Reply With Quote
09-08-08, 02:14 PM   #16
kerrang
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2006
Posts: 109
Sorry - yesterday was a long and miserable day of coding and I was snappy - I shouldn't have been so snippy!!

I'm amazed you got that to work too - in every case I tried (and I tried a LOT!) it broke before renaming every element for me...

The best it ever did was rename 9 out of 15 entries I had in the table at the time - reload would rename another few and so on....

I'd assumed that pairs() somehow 'remembered' where it was in the table (using a pointer or somesuch thing) - so if you changed an element it was left with no idea where to go next...

You're also ADDING keys into the table which pairs() may - or may not - be aware of???

This feeds into something else I'm interested in but can find no reference for - which is what ORDER something like pairs() will go through a (non numeric-indexed) table.

As best as I can tell it's random - I have a table (also a savedvariable) the content of which seldom changes - but the function I use to copy it's values into a string produces a different string every time I use it!!

e.g.

Table keys "FRED", "BARNET", "WILMA" will produce

"FredBarnetWilma"

and then

"BarneyFredWilma"

after the next reloadui and then

"WilmaFredBarney"

and so on...

I know there are ways of sorting a table but I just wondered how it dealt with it normally??
  Reply With Quote

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

Thread Tools
Display Modes

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