3

Given is the string:

local a = "#5*$4a+02/+2-%110"

Now i want to split the following string into a table like

b[1]="5" 
b[2]="*" 
b[3]="$4a" 
b[4]="+" 
... 
b[10]="%110"

My solution was

local b = {}

for part in string.gmatch(a,"[%x%$%%]+[%+%-%/%*]+") do 
    b[#b+1] = part 
end

But i get

   b[1]="5*"
   b[2]="$4a+"
   b[3]="02/"
   ...

but without the last number: %110.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
a.c.m.
  • 31
  • 2

2 Answers2

0

You mean:

string.gmatch(a,"[%x%$%%]+[%+%-%/%*]+[%x%$%%]")

This result was an empty Table. I want to caclulate the formula in that string.

local a = "#5*$4a+02/+2-%110"

The problem is now, a eval like

function evalStr(str)
   local f= load ('return ('..str..')')
   return f()
end

can calculate this Formula only with Decimal-Digits. Here i it is mixed with Hexadecimal and Binary Digits. So i have to to convert the hexadecimal and binary digits to decimal to calculate the formula.

a.c.m.
  • 31
  • 2
0
local a = "#5*$4a+02/+2-%110"
local b = {}

for part in string.gmatch(a,"[^#][%x%%%$]*[%+%-%*/]*") do
    b[#b+1] = part:match("[%w%%%$]+")
    b[#b+1] = part:match("[%+%-%*/]")
    
end

for k,v in pairs(b) do
    print(v)

end
a.c.m.
  • 31
  • 2