1. Firstly, I apologize for my ignorance in these questions, but these are the first areas in this book that make absolutely no sense to me.

    I may be overthinking something, but I don't particularly understand what "captures" are in terms of this code.

    Page 97, Table 6-2:

    string.format("%05d", 17) ------------------------------------ 00017

    string.format("%6.3f", 13) ------------------------------------ 13.000

    string.format("%s", "monkey") ----------------------------- monkey

    string.format("%10s", "monkey") -------------------------- monkey

    string.format("%5.3s", "monkey") ------------------------- mon

    I may just not be understand the options mentioned on Page 96's list, but I don't understand what the numbers between the percent sign and the type specifier mean.

    Page 103, Table 6-7:

    "(%-?%d+%.?%d*)" --------- I guess I don't understand entirely how these character specifiers can be mashed together in this way.

    "%S+$" -------------------------- Other than being specified as a "magic character" on Page 99, I don't see anywhere where the functionality of "$" is explained. What does it mean in this situation?

    Page 104:

    print(string.gsub("hello world", "(%w+)", "%1 %1")) ------------- What does the italicized portion (%1 %1) mean?

    Firstly, I apologize for my ignorance in these questions, but these are the first areas in this book that make absolutely no sense to me.

    I may be overthinking something, but I don't particularly understand what "captures" are in terms of this code.

    Page 97, Table 6-2:

    string.format("%05d", 17) ------------------------------------ 00017

    string.format("%6.3f", 13) ------------------------------------ 13.000

    string.format("%s", "monkey") ----------------------------- monkey

    string.format("%10s", "monkey") -------------------------- monkey

    string.format("%5.3s", "monkey") ------------------------- mon

    None of these have 'captures' at all, so I'm not sure why that's come up. Captures only happen when you are using the 'find' functions, such as match, find, gmatch, gsub, etc. In this case you are CONSTRUCTING strings, so there are no captures. The format string %05d says format the first argument as an integer, with a width of 5 characters, padded with 0s on the left.

    The other examples here are similar.

    I may just not be understand the options mentioned on Page 96's list, but I don't understand what the numbers between the percent sign and the type specifier mean.

    Page 96 discusses these explicitly. The paragraph that begins with "Several options can be used in a conversion specification..."

    Page 103, Table 6-7:

    "(%-?%d+%.?%d*)" --------- I guess I don't understand entirely how these character specifiers can be mashed together in this way.

    There's not really a question here. This string means (from left-to-right):

    1. ( Left parenthesis means start a capture here
    2. %-? means look for the actual character '-', which may or may not be included
    3. %d+ means look for at least one of numerical digits, as many as can be found
    4. %.? means look for the actual character '.', which may or may not be included
    5. %d* means look for any numerical digits, possibly 0
    6. ) Right parenthesis means end the capture here

    How else would this be specified? I don't understand your question about 'being mashed together'. If you included spaces or anything else, then those spaces would have to exist in the string being matched as well. Patterns are literal.

    "%S+$" -------------------------- Other than being specified as a "magic character" on Page 99, I don't see anywhere where the functionality of "$" is explained. What does it mean in this situation?

    The top of page 102 discusses 'Pattern Anchors'. Table 6-7 also includes an example of both anchors (^ and $).

    Page 104:

    print(string.gsub("hello world", "(%w+)", "%1 %1")) ------------- What does the italicized portion (%1 %1) mean?

    The second paragraph (first full paragraph) at the top of Page 104 explains this. In a replacement pattern, the string %0 means the entire capture from the pattern, whereas %1 through %9 means the nth capture. This code is saying:

    1. Print the result of the following
    2. Search for all instances of (%w+) in the string hello world and replace them with the replacement string %1 %1. Further explanation:
      • (%w+) is a pattern that matches 1 or more alphanumeric characters, as many as can be matched at one time.
      • the string "hello world" should be self explanatory.
      • the replacement string `"%1 %1" says to replace the entire matched string with the first capture, followed by a space, followed by the first capture again.

    This is why the function overall prints "hello hello world world".

    string.format("%6.3f", 13) = 13.000

    string.format("%s", "monkey") = monkey

    string.format("%10s", "monkey") = monkey

    string.format("%5.3s", "monkey") = mon

    Thanks for your help with the other areas, but I'm still having trouble understanding these four lines. I've read over the paragraphs on Page 96.

    I understand that line 1 means that there will be 3 decimal digits added for precision, in this case being 3 zeros. But what does the "6" specify? Is it width, meaning that there would be an invisible space character after the last zero?

    Does line 2 mean anything different than:

    print("monkey")

    For the 3rd line, is this specifying width again, meaning 4 invisible space characters after "monkey"?

    And, I guess my question is the same for the 4th line pertaining to the "5" part of the character specifier.

    string.format("%6.3f", 13) = 13.000

    string.format("%s", "monkey") = monkey

    string.format("%10s", "monkey") = monkey

    string.format("%5.3s", "monkey") = mon

    Thanks for your help with the other areas, but I'm still having trouble understanding these four lines. I've read over the paragraphs on Page 96.

    I understand that line 1 means that there will be 3 decimal digits added for precision, in this case being 3 zeros. But what does the "6" specify? Is it width, meaning that there would be an invisible space character after the last zero?

    It's width, with 3 decimal places of precision. The decimal point counts as a character of width. There's nothing invisible.

    Does line 2 mean anything different than:

    print("monkey")

    Yes, because string.format() returns a string, it doesn't print it. However, string.format("%s", "monkey") == "monkey". These are just VERY simple examples of format strings, you would use them in more complex formats in reality.

    For the 3rd line, is this specifying width again, meaning 4 invisible space characters after "monkey"?

    They're not invisible, they're there. The return value is "monkey ".

    And, I guess my question is the same for the 4th line pertaining to the "5" part of the character specifier.

    This specifies that the width of the string should be 5 characters, and that you should only use the first 3 characters. Hence you get " mon"

    Alright, thanks. Now, I understand. Great read, by the way. Picked this up two days ago, and I can't put it down. I was bound to hit some place that I had absolutely no comprehension in. :D