WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   String parse into frame names (https://www.wowinterface.com/forums/showthread.php?t=59239)

glupikreten 10-01-22 04:05 AM

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

kurapica.igas 10-01-22 08:16 AM

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))

glupikreten 10-01-22 11:23 AM

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 :)

Xrystal 10-01-22 04:11 PM

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

SDPhantom 10-02-22 01:16 PM

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.

glupikreten 10-03-22 04:23 AM

Thank you all kindly


All times are GMT -6. The time now is 08:05 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI