View Full Version : Counting characters with lua
johnp
February 18th, 2009, 09:05 PM
Hi,
I am trying to count the instance of a double quote character in a text string.
I tried string.find, but it only returns the first instance and string.match or string.gmatch do not seem to be supported.
Any idea what I can do?
Rob H
February 19th, 2009, 02:09 AM
string.match and string.gmatch are from Lua 5.1, Girder uses Lua 5.0 which has string.gfind which should do what you want.
johnp
February 19th, 2009, 05:09 AM
ahh thanks. I'll try that one.
johnp
February 19th, 2009, 07:28 AM
Hi,
I tried this and is seems to do what I wanted for the first problem which is to count the total number of quotation marks in a string
local mynum = 0
while mynum == 0 do
for q in (string.gfind(MoviePath, '"')) do
mynum = mynum + 1
end
local MovieIndex = mynum/2
print ("total movies = "..MovieIndex)
end
Now I need to find within each string, the text between each quotation mark.
string.find gives me the first match, but I am not sure hot to iterate that over the entire string.
Any ideas on how to do that??
Thanks,
Rob H
February 19th, 2009, 07:50 AM
You could do something like this
for q in string.gfind(s, [["(.-)"]]) do
-- do something with the value
end
Note that I'm using [[ and ]] to delimit the pattern string here.
johnp
February 19th, 2009, 10:03 AM
Thanks.
I am still struggling a bit. Perhaps a practical example would serve as a better way to express what I am trying to do.
I have a string that may look like this:
"M:\Video\A Scanner Darkly\VIDEO_TS\VIDEO_TS.IFO" "M:\Video\Amistad\VIDEO_TS\VIDEO_TS.IFO" "M:\Video\Backdraft\VIDEO_TS\VIDEO_TS.IFO" "M:\Video\Betterman, Vol. 1, The Awakening\VIDEO_TS\VIDEO_TS.IFO"
It contains a series of movie titles that are enclosed in pairs of double quotes " " which need to be retained and passed on down the line to another program later on. So what I need to do is take the string above, and create a set of variables based on the individual titles. Using this string above as an example:
Movie1 = "M:\Video\A Scanner Darkly\VIDEO_TS\VIDEO_TS.IFO"
Movie2 = "M:\Video\Amistad\VIDEO_TS\VIDEO_TS.IFO"
Movie3 = "M:\Video\Backdraft\VIDEO_TS\VIDEO_TS.IFO"
Movie4 = "M:\Video\Betterman, Vol. 1, The Awakening\VIDEO_TS\VIDEO_TS.IFO"
What might be helpful, (I think) is that I can format the string at the front end to have a delimiter say ## in between the titles, but I am still stuck on how to separate each title out. I am supremely confident it can be done in Lua, but I just have no idea as to how to go about it.
As always, any help is humbly accepted.
Rob H
February 19th, 2009, 10:14 AM
The code above works for me with that data.
Actually, I tested it with
for q in string.gfind(s, [["(.-)"]]) do
print(q)
end
johnp
February 19th, 2009, 10:28 AM
Hmmm, I'll try it again and see where I went worng. I thought I did the same thing with the print statement, but who knows.
johnp
February 19th, 2009, 10:49 AM
OK I see now.
I tried to integrate it into my similar for loop and broke it.
My first question is how does that work?
The trickery appears to be in the [["(.-)"]] part. What I see is a pattern, encapsulated in quotes, then (.-) which must mean everything in between?? Am I close?
At any rate, now I get each movie title, with the quotes stripped, so I restored those by changing it to this:
for m in string.gfind(MoviePath, [["(.-)"]]) do
print('"'..m..'"')
end
Now all I need is the way to create the variables. The first line should go to Movie1 variable, the 2nd to Movie2 etc. etc.
How would one go about that?
Thanks for your patience.
johnp
February 19th, 2009, 11:15 AM
OK, so I'm getting closer.
movievarnum = 1
for m in string.gfind(MoviePath, [["(.-)"]]) do
movievar = "Movie"..movievarnum
print('"'..m..'"')
print (movievar)
movievarnum = movievarnum+1
end
This prints out the titles followed by the name I'd like for each variable. The kicker is I don't know how to get a variable name to be assigned from another variables value. Is that even possible?
johnp
February 19th, 2009, 12:07 PM
movievarnum = 1
for m in string.gfind(MoviePath, [["(.-)"]]) do
if movievarnum == 1 then
Movie1 = ('"'..m..'"')
elseif movievarnum == 2 then
Movie2 = ('"'..m..'"')
elseif movievarnum == 3 then
Movie3 = ('"'..m..'"')
elseif movievarnum == 4 then
Movie4 = ('"'..m..'"')
elseif movievarnum == 5 then
Movie5 = ('"'..m..'"')
end
movievarnum = movievarnum+1
end
Any thoughts on how to do this more efficiently? Otherwise it works as is.
Thanks for everything.
Rob H
February 20th, 2009, 03:17 AM
You should use a table for that e.g.
Movies = {}
then later use
Movies[movievarnum] = m
johnp
February 20th, 2009, 04:48 AM
Yes, I like that approach much better.
Thank you very much!
Rob H
February 20th, 2009, 05:44 AM
By the way, if you want to retain the quotes just move them inside the parentheses in the search pattern (the parentheses indicate what is known as a "capture").
johnp
February 20th, 2009, 09:13 AM
Excellent, even cleaner still.
Thanks again.
Powered by vBulletin® Version 4.1.8 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.