Thread Tools Display Modes
10-01-22, 04:05 AM   #1
glupikreten
A Theradrim Guardian
Join Date: Apr 2009
Posts: 60
String parse into frame names

Hi,

How would i parse this into proper condition?

Code:
local condition
line = 'frame1 and frame2 or frame3'

for token in string.gmatch(line, "[^%s]+") do

   if token == "and" or token == "or" then 
      token = token.." "
   else 
      token = '_G['..token..'] '
   end
   condition =  condition .. token
end
Thank you

Last edited by glupikreten : 10-01-22 at 04:20 AM.
  Reply With Quote
10-01-22, 08:16 AM   #2
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
You can use gsub to do the replacement

Lua Code:
  1. line = 'frame1 and frame2 or frame3'
  2.  
  3.  
  4. local keywords = {
  5.     ["and"] = "and",
  6.     ["or"] = "or",
  7. }
  8.  
  9. print(line:gsub("[_%w]+", function(word)
  10.     return keywords[word] or string.format("_G[%q]", word)
  11. end))
  Reply With Quote
10-01-22, 11:23 AM   #3
glupikreten
A Theradrim Guardian
Join Date: Apr 2009
Posts: 60
Thank you .. but it doesn't go thru if...

It is not considered as "FRAME and FRAME" but "string with spaces" if you know what i mean.

i ended up doing this... this seems to be working

lua Code:
  1. local keywords = {
  2.     ["and"] = "and",
  3.     ["or"] = "or",
  4. }
  5.  
  6. function f(condition, callback)
  7.     condition_table = {}
  8.     condition_table['and'] = {}
  9.     condition_table['or'] = {}
  10.  
  11.     condition:gsub(
  12.         '[_%w]+',
  13.         function(word)
  14.             -- return keywords[word] or string.format("_G[%q]", word)
  15.             -- text.keywords[word] = string.format('_G[%q]', word)
  16.             if keywords[word] then
  17.                 next_keyword = keywords[word]
  18.             else
  19.                 if next_keyword then
  20.                     table.insert(condition_table[next_keyword], word)
  21.                 else
  22.                     table.insert(condition_table, word)
  23.                 end
  24.             end
  25.         end
  26.     )
  27.  
  28.     for k, v in ipairs(condition_table) do
  29.         c = _G[v]
  30.     end
  31.     for k, v in ipairs(condition_table['and']) do
  32.         c = c and _G[v]
  33.     end
  34.     for k, v in ipairs(condition_table['or']) do
  35.         c = c or _G[v]
  36.     end
  37.  
  38.     if(c) then
  39.         callback()
  40.     end
  41. end

I'm faaaaar from lua developer.. I'm struggling a lot... so if anyone have better solution I'm game

Last edited by glupikreten : 10-01-22 at 12:04 PM.
  Reply With Quote
10-01-22, 04:11 PM   #4
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,928
If you are talking about strings with just a bunch of spaces in you could used ( strtrim(string[, chars]) - Trim leading and trailing spaces or the characters passed to chars from string. ) and then check to see if it is an empty string.

I got that from this page : https://wowpedia.fandom.com/wiki/Lua...ring_Functions
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
10-02-22, 01:16 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
If the string is valid Lua syntax, you can run it through loadstring().

Code:
loadstring("return frame1 and frame2 or frame3")();
Note: loadstring() returns a function if it compiles successfully. This is why there's an empty set of parenthesis at the end of the line. This takes the returned function and calls it on the spot. Be aware loadstring() returns nil and an error string if there's a syntax error. You should have a way of handling this, but this is just a basic example of its use.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
10-03-22, 04:23 AM   #6
glupikreten
A Theradrim Guardian
Join Date: Apr 2009
Posts: 60
Thank you all kindly
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » String parse into frame names

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