Patterns expert here on the forum?

Hi, I have never worked with patterns before and it seems a way to magical for me to get a hang of it…

t = {}  
i = 1  
  
s = "from=world, to=Lua\_1"  
  
 t[i] = {}  
  
for k, v in string.gmatch(s, "(%w+)=(%w+)") do  
  
 print(k,v)  
  
end  

As you see, I am having a value of Lua_1 but that won’t be recognized by the gmatch function. I want all values to be valid through the patter no matter of how many underscores and digits I have…

Any ideas? [import]uid: 81188 topic_id: 25780 reply_id: 325780[/import]

What is a pattern? Ok, now you know from my response that you won’t get an answer from me.

I want to know what this technical wizardry you speak of is!

So could you give a longer example of what you are trying to do? I see you are stating that all values should be valid through out the pattern no matter how many underscores and digits you have.

What does that mean, could you give an example in english :slight_smile:

I’m trying to picture what you are saying. Someone else will have to chime in to give you the programmatic guidance, I just want to know what you are trying to do :slight_smile:
-ng [import]uid: 61600 topic_id: 25780 reply_id: 104224[/import]

Well, I am trying to parse a large amount of text into a lua table. This textfile includes parameters and values. This is a example of one row from the file:

Name=Staging,refid=1,img=wildebeest1\_shd\_1,yref=3.8000000000000114,xref=6.975,yscale=1,xscale=1,alpha=1,y=150.8,x=7.85,rotation=0,frame=1  

If you look at the value for the parameter “img” in the sample above, you see that it has an value of “wildebeest1_shd_1”. I never know what the format is, so I just want the gmatch function to return everything thats present after the = and before the , sign in the data chunk.

So I want the gmatch to return the following pairs when I loop through the data:

PAIR A                  PAIR B

Name                    Staging
refid                     1
img                      wildebeest1_shd_1
yref                      3.8000000000000114
xref                      6.975
yscale                  1
xscale                  1
alpha                  1
y                          150.8
x                          7.85
rotation                    0
frame                       1

Joakim
[import]uid: 81188 topic_id: 25780 reply_id: 104229[/import]

I think what you want is to use sets and complementary sets to fine-tune your regular expression matching, e.g. in your sample code above, do:

[lua]t = {}
i = 1

s = “from=world, to=Lua_1”

t[i] = {}

for k, v in string.gmatch(s, “([%w]+)=([%w%p][^,]+)”) do

print(k,v)

end
–>from world
–>to Lua_1[/lua]

Break that second term down:

lua[/lua]

There are two sets, [%w%p] and [^,]. The first says “any alphanumeric character (%w) and punctuation character (%p)” and the second says “not a comma”. This might be confusing because the “^” character outside a bracketed set has a different meaning (i.e., “match the beginning of the string”). You put these two sets together, though, and you have a pattern that says “match any alphanumeric or punctuation character except a comma.”
[import]uid: 44647 topic_id: 25780 reply_id: 104228[/import]

@Toby - exactly what I was looking for - thanks, you made my day!

Joakim [import]uid: 81188 topic_id: 25780 reply_id: 104230[/import]

On the other hand, if you’re in control of the formatting of the string data you want put into a lua table, you could do it in JSON. [import]uid: 44647 topic_id: 25780 reply_id: 104231[/import]

@Toby - I tried to do that with JSON but it goes way to slow so therefore I am trying to parse the data by my self. And my early test has indicated that I can cut down the loading time from 0.7 sec to 0.17 sec.
But I have more problems now,

As you see - my data string is holding more then 2 pairs and I am only getting out two pairs with the match. Any ideas on how to get out all data?

Joakim [import]uid: 81188 topic_id: 25780 reply_id: 104236[/import]

The problems seems to be that numbers doesn’t get caught with the gmatch…any more ideas?

Joakim [import]uid: 81188 topic_id: 25780 reply_id: 104239[/import]

Change the “+” to a “*” in the second term:

[lua]t = { {} }
s = “Name=Staging,refid=1,img=wildebeest1_shd_1,yref=3.8000000000000114,xref=6.975,yscale=1,xscale=1,alpha=1,y=150.8,x=7.85,rotation=0,frame=1”

for k, v in string.gmatch(s, “([%w]+)=([%w%p][^,]*)”) do

print(k,v)

end
–>Name Staging
–>refid 1
–>img wildebeest1_shd_1
–>yref 3.8000000000000114
–>xref 6.975
–>yscale 1
–>xscale 1
–>alpha 1
–>y 150.8
–>x 7.85
–>rotation 0
–>frame 1[/lua] [import]uid: 44647 topic_id: 25780 reply_id: 104243[/import]

@Toby - that is great - it works as it should and my small test with the loading time is ten times faster then using Json. I really appreciate your help.

Since I am having you on the line and you are a great guy - can you maybe help me with this last thing, so that I don’t have to go through all my old code to change it…

As I told you, I am building up an lua table from the data. The table I am having now, is not by the same format as it was when I was using JSON. Well, I can go over all my code but that would be a lot of work.

Before I could address my array like this:

print(tmp_data[i].Name)

but now it doesn’t seems to work… I can’t access the “param” as I used to.

I am building up my array like this:

 local table = {}  
 --Reading and splitting in all rows from the file  
 table = string.split( parseData("animaldata/text.txt"),"|")  
  
 --Go through all lines and build the array  
 for i = 1,#table, 1 do  
 s = table[i]  
 animArr[i] = {}  
 for k, v in string.gmatch(table[i], "([%w]+)=([%w%p][^,]\*)") do  
 animArr[i][k] = v  
 end  
 end  

Thanks, Joakim [import]uid: 81188 topic_id: 25780 reply_id: 104248[/import]

Forget that, it was working!

Thanks again!

Joakim

[import]uid: 81188 topic_id: 25780 reply_id: 104249[/import]