Thread Tools Display Modes
08-05-13, 02:34 PM   #1
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
Question about parsing

So I'm working on my little debugging add-on, and I want to make it possible to watch values inside tables, so I want to be able to take a string like "mytable[a][b][c]" and extract the list of {"a","b","c"}.
Is there any way to do that in one go, or do I have to iterate over the string? And, for that matter, what's the best way to iterate (if I must)?

(I thought string.match would let me do it in one go, but if it can then I haven't been able to find out how. Nor how to iterate with it properly.)
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!
  Reply With Quote
08-05-13, 04:29 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Your post is not very clear. Can you post a code example of what kind of table structure you have, and what you want to "watch" (eg. values being added, values being removed, values being changed, values being read in response to an event, etc.)?
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-05-13, 04:57 PM   #3
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
Sorry if I was unclear.

I have a string, for example "mytable[a][b][c]", and I want a simple way of parsing the list {"a","b","c"} out of it.
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!
  Reply With Quote
08-05-13, 05:17 PM   #4
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
You're just restating what you said in your first post. The reason Phanx wants more information is because after we post a solution to do what you asked for you're going to come back with another example where it doesn't work because you weren't clear about what you wanted.

The fact that you have tables stored as strings in the first place is a little suspect.

Do you really want to turn "mytable[a][b][c]" into a two dimensional list or do you want the table to retain its original structure?

Last edited by semlar : 08-05-13 at 05:19 PM.
  Reply With Quote
08-05-13, 05:19 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Well, that's exactly what you already wrote that wasn't very clear -- in fact, it looks like you just copied and pasted from your first post, which is what I already read and said was unclear -- but I guess I'll assume that your string is not actually a table, and has nothing to do with any actual table that exists anywhere in your code, but you want to parse out all of the table-key-like strings from it and insert them into an indexed table, in which case:

Code:
local str = "mytable[a][b][c]"
local t = {}
for v in string.gmatch(str, "%[(.-)%]") do
   tinsert(t, v)
end
Results in :

Code:
t = {
   "a",
   "b",
   "c",
}
However, as Semlar said, this is a horrible way to store data. If you want to store tabular data, just store it as a table.

If the above is not relevant to what you need, please actually restate your question in a different way (not just copy/paste the same vague thing) and provide a code example.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-05-13, 05:48 PM   #6
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
Thanks, that's exactly what I needed.

I still don't understand what was unclear though. This was a question about parsing strings, not data storage (what do you mean, store tables as strings?). The add-on in question is this, if that sheds any extra light on the situation.
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!
  Reply With Quote
08-05-13, 06:20 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Malsomnus View Post
I still don't understand what was unclear though. This was a question about parsing strings, not data storage (what do you mean, store tables as strings?).
Even you don't know what was unclear to someone, repeating word-for-word what you already said isn't going to make it any less unclear to them. Just restate the question in a different way.

In this case, it was unclear to me because you seemed to be asking about string manipulation, but you also posted what looked like a table lookup, so I wasn't sure if you were talking about a string that looked like a table lookup, or you were talking about an actual table lookup. Since you didn't post any code, it could have gone either way.

Anyway, glad my assumption was relevant and you were able to solve your problem.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-05-13, 07:47 PM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Yeah, semlar and I were both confused, too.
__________________
"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
08-06-13, 08:46 AM   #9
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
I think what malsomnus is trying to accomplish is some way of implementing a simple query language(like xpath or sizzle) so one can enter a string to quickly navigate to a specific table.
  Reply With Quote
08-06-13, 09:16 AM   #10
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
Originally Posted by ravagernl View Post
I think what malsomnus is trying to accomplish is some way of implementing a simple query language(like xpath or sizzle) so one can enter a string to quickly navigate to a specific table.
Currently my add-on lets you, for example, look at the contents of a global table name mytable by typing WatchVar('mytable').
What I want to do is, suppose the table is really big and complex and at the moment I want to look at just one specific part of it, then I should be able to type WatchVar('mytable[a][b][c]') so I don't have to navigate all the way in there, plus have 3 levels of the table clutter up my screen.
In retrospect, I think that trying to explain the context did nothing except confuse people...
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!
  Reply With Quote
08-06-13, 11:53 AM   #11
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Just to be clear, you know that /dump exists? You just want a different way to do this?
__________________
"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
08-06-13, 12:38 PM   #12
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
You could also probably just do something like loadstring("return mytable[a][b][c]")() to get a reference to the table.
  Reply With Quote
08-06-13, 12:47 PM   #13
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
Originally Posted by Seerah View Post
Just to be clear, you know that /dump exists? You just want a different way to do this?
I haven't heard of it, actually. This is good to know.
(Is there a nice organized list of all those useful-for-developers slash commands anywhere? I haven't been able to find one and it annoys me often)

I hope you'll agree though that the way my add-on does it is somewhat better than mere debug prints, in many cases.

Originally Posted by semlar View Post
You could also probably just do something like loadstring("return mytable[a][b][c]")() to get a reference to the table.
I considered something similar, but eventually decided I prefer to do this tiny bit of parsing and know what table it all comes from. It will be useful for a couple of features I'm thinking of adding.

[Edit]
Thanks, by the way, I've been wondering what the function to evaluate a string is called here
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!

Last edited by Malsomnus : 08-06-13 at 12:51 PM.
  Reply With Quote
08-06-13, 04:52 PM   #14
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Malsomnus View Post
Currently my add-on lets you, for example, look at the contents of a global table name mytable by typing WatchVar('mytable').
What I want to do is, suppose the table is really big and complex and at the moment I want to look at just one specific part of it, then I should be able to type WatchVar('mytable[a][b][c]') so I don't have to navigate all the way in there, plus have 3 levels of the table clutter up my screen.
In retrospect, I think that trying to explain the context did nothing except confuse people...
Just explaining what you want to do in general (as in the above quote) instead of vaguely describing a very specific example with no context would have been 1000x better.

In addition to the built-in /dump command, you may also want to take a look at other existing addons that offer similar features, eg. Lua Browser, WatchIt, or Spew (though I'm not sure if that last one still works).
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-06-13, 05:09 PM   #15
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
Originally Posted by Phanx View Post
Just explaining what you want to do in general (as in the above quote) instead of vaguely describing a very specific example with no context would have been 1000x better.
I think I'm not very good at all this "talking to humans" thing. I'll try harder next time!

In addition to the built-in /dump command, you may also want to take a look at other existing addons that offer similar features, eg. Lua Browser, WatchIt, or Spew (though I'm not sure if that last one still works).
I'll probably check Lua Browser out of curiousity... but I have to point out (and I'm giggling pretty hard while typing this) that WatchIt is my add-on which this thread is about
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!
  Reply With Quote
08-06-13, 06:43 PM   #16
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Another thing that would have been helpful.
__________________
"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

WoWInterface » Developer Discussions » Lua/XML Help » Question about parsing


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