Starfinder Playlist
  1. #1
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075

    Lua string.find() question

    I recall reading up on Lua string functions but oddly, I never noticed the discrepancy until this morning.

    I noticed that the return values from string.find() were being assigned to 4 variables. Looked again and saw it is indicated (in the search results I hit) that find() returns start and end of match only. Two values. All the examples show this and how to then user string.sub() to get the contents of the match.

    But obviously, if you print the following:
    Debug.console(string.find(
    sParams, "([^%s]+)%s*(.*)"))

    You can see (assuming match) something like:
    [5/11/2021 12:10:02 PM] #1|#11|s'1d6-1|'s'd6-1'

    Which seems to imply the return values are: start, end, string being searched, matching part of the search (normally retrieved with sub())

    Now, that is fine (and greatly convenient) and I should move on but I just wanted to know where I SHOULD be looking to get right Lua documentation then because googling is steering me wrong in at least this situation and maybe more I didn't know about.

  2. #2
    Can use this:
    https://www.lua.org/pil/20.1.html

    Or this:
    https://riptutorial.com/lua/example/...-introduction-

    In terms of pattern matching any lua reference site cover it in one way or the other. It's not the easiest, so you might have to tinker.
    Dominic Morta
    Ruleset Developer
    Smiteworks

    How to zip up your campaign if the Developers ask for it-How to zip up your campaign if the Developers ask for it

    How to provide an Unity Connection issue?-Connection Issues and What to Provide

    Unity Updater issue?-Updater Issues

    Classic and Unity Port Forwarding?-Fantasy Grounds Connections Explained

    Comcast or Cox ISP User?-Comcast XFinity and Cox Users

    Have a suggestion?-Feature Request

  3. #3
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    That’s my point though

    That was the exact website and page I was reading. It declares that find returns two values as I said.

    Code:
    The basic use of string.find is to search for a pattern inside a given string, called the subject string. The function returns the position where it found the pattern or nil if it could not find it. The simplest form of a pattern is a word, which matches only a copy of itself. For instance, the pattern 'hello' will search for the substring "hello" inside the subject string. When find finds its pattern, it returns two values: the index where the match begins and the index where the match ends.
    
        s = "hello world"
        i, j = string.find(s, "hello")
        print(i, j)                      --> 1    5
        print(string.sub(s, i, j))       --> hello
        print(string.find(s, "world"))   --> 7    11
        i, j = string.find(s, "l")
        print(i, j)                      --> 3    3
        print(string.find(s, "lll"))     --> nil
    When a match succeeds, a string.sub of the values returned by string.find would return the part of the subject string that matched the pattern. (For simple patterns, this is the pattern itself.)
    Unless I misread it but the prints they show return only 2 values. Is it instead because I did Debug.console() that adds stuff?


    I’m not concerned with pattern matching here. Just return values. I get how this works and it being a limited Tegel of sorts etc. Butvreading that page, I expect only the return of “#1 #11”

  4. #4
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,362
    You've added some string captures into the pattern - anything between non-escaped brackets are returned (if there's a match). You have two sets of (...) in there - which is returning an additional variable for each.
    Last edited by Trenloe; May 11th, 2021 at 18:21.
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  5. #5
    The first two values of the return are the start and end positions. Any continued returned values would depend on your pattern matching. So yes, you should be concerned with pattern matching as this is all part of that mechanism.
    Dominic Morta
    Ruleset Developer
    Smiteworks

    How to zip up your campaign if the Developers ask for it-How to zip up your campaign if the Developers ask for it

    How to provide an Unity Connection issue?-Connection Issues and What to Provide

    Unity Updater issue?-Updater Issues

    Classic and Unity Port Forwarding?-Fantasy Grounds Connections Explained

    Comcast or Cox ISP User?-Comcast XFinity and Cox Users

    Have a suggestion?-Feature Request

  6. #6
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    THAT'S IT!

    Thanks man - I was just editing something and looked up find to see order of args and aw just 2 args... it didn't occur to me that it was capturing anything because I just looked at it as a matching pattern without looking at details. I was editing existing code to add some optional params for saving throws / half/none etc when I misinterpreted this.

    I figured I was either looking at an older wiki than the version we used or I was misreading it but I couldn't see what I was misreading... because.I.didn't.READ.it.really obviously.

  7. #7
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    Quote Originally Posted by superteddy57 View Post
    The first two values of the return are the start and end positions. Any continued returned values would depend on your pattern matching. So yes, you should be concerned with pattern matching as this is all part of that mechanism.

    I was responding to Trenloe before this one showed up - you are 100% correct. It was just that it was obvious to you that there was a capture. I have used captures all over (hate writing regex... makes head hurt lol as you intimated may be the case for some) but I stupidly did not read the details of the match string. I didn't even notice ()s ... monumental oversight especially since I am aware and used captures. By adding a capture it was not longer a simple "find()"

    I think part of my problem is C++ (and Java, I guess, now...) to scripting change. Functions can be a bit more fluid than in those others (especially pre tuples which while I am familiar with, 99.9% of all I did was C++98/03

    TOTAL D'OH moment for me thanks all for your patience.

  8. #8

  9. #9
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    Oh yeah, I know - which only makes it worse for me since I only barely know regex (now see what “tegal” was, the phone spell checking my regex…) and when in past months I was working on new thac0 I ran not issues some things it was among the unsupported features if I recall.

    Tgg he años to you guys, I realize what I misunderstood and my “huh” became /blush 8)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Log in

Log in