Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Counting characters with lua

  1. #1
    Join Date
    Oct 2002
    Location
    Waterford, WI USA
    Posts
    171

    Default Counting characters with lua

    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?
    Regards,

    John

  2. #2
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    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.
    --Rob

  3. #3
    Join Date
    Oct 2002
    Location
    Waterford, WI USA
    Posts
    171

    Default

    ahh thanks. I'll try that one.
    Regards,

    John

  4. #4
    Join Date
    Oct 2002
    Location
    Waterford, WI USA
    Posts
    171

    Default

    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

    Code:
    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,
    Regards,

    John

  5. #5
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    You could do something like this

    Code:
    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.
    --Rob

  6. #6
    Join Date
    Oct 2002
    Location
    Waterford, WI USA
    Posts
    171

    Default

    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.
    Regards,

    John

  7. #7
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    The code above works for me with that data.

    Actually, I tested it with

    Code:
    for q in string.gfind(s, [["(.-)"]]) do
      print(q)
    end
    --Rob

  8. #8
    Join Date
    Oct 2002
    Location
    Waterford, WI USA
    Posts
    171

    Default

    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.
    Regards,

    John

  9. #9
    Join Date
    Oct 2002
    Location
    Waterford, WI USA
    Posts
    171

    Default

    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:

    Code:
    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.
    Regards,

    John

  10. #10
    Join Date
    Oct 2002
    Location
    Waterford, WI USA
    Posts
    171

    Default Almost there

    OK, so I'm getting closer.

    Code:
    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?
    Last edited by johnp; February 19th, 2009 at 02:53 PM. Reason: bad typo
    Regards,

    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •