Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 1 | SYNOPSIS |
| 2 | int sscanf(string str, string fmt, mixed var1, mixed var2, ...) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 3 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 4 | DESCRIPTION |
| 5 | Parse a string str using the format fmt. fmt can contain |
| 6 | strings seperated by %d and %s. Every %d and %s corresponds to |
| 7 | one of var1, var2, ... . |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 8 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 9 | The match operators in the format string have one of these |
| 10 | formats: |
| 11 | %[+][!|~][<size>[.<minmatch>]]<type> |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 12 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 13 | <type> may be: |
| 14 | d: matches any number. |
| 15 | D: matches any number. |
| 16 | U: matches any unsigned number. |
| 17 | s: matches any string. |
| 18 | %: matches the % character. |
| 19 | t: matches whitespace (spaces and tab characters), but does |
| 20 | not store them (the simple ' ' matches just spaces and |
| 21 | can't be given a size specification). |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 22 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 23 | <size> is the expected field size, <minmatch> the demanded |
| 24 | minimal match length (defaults are 0 for strings and 1 for |
| 25 | numbers). Each of these both may be specified numerically, or |
| 26 | as '*' - then the value of the variable at the current place |
| 27 | in the argument list is used. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 28 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 29 | Specifying + will require that the characters after the field |
| 30 | match as well, or the match will be deemed unsuccessful (the variable |
| 31 | might still get assigned, though). |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 32 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 33 | Specifying ! will perform the match, but neither store the |
| 34 | result nor count the match. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 35 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 36 | Specifying ~ will perform and count the match, but not store |
| 37 | the result. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 38 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 39 | If the %s specifier is not at the end of the format string, |
| 40 | it is matched only if the following character(s) or format |
| 41 | is found, too. See below for an example. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 42 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 43 | The difference between %d and %D/%U is that the latter will abort |
| 44 | an immediately preceeding %s as soon as possible, whereas the |
| 45 | former will attempt to make largest match to %s first. |
| 46 | %D/%U will still not skip whitespace, use %.0t%D to skip optional |
| 47 | whitespace. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 48 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 49 | If a number is matched that exceeds the numerical limits of |
| 50 | integers the match is deemed unsuccessful. |
Zesstra | 5481d49 | 2021-04-08 20:07:06 +0200 | [diff] [blame] | 51 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 52 | The number of matched arguments will be returned. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 53 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 54 | The function sscanf is special, in that arguments are passed |
| 55 | by reference automatically. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 56 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 57 | EXAMPLES |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 58 | string who, what; |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 59 | if (sscanf("throw frisbee to rover", |
| 60 | "throw %s to %s", what, who) != 2) |
| 61 | write("Usage: throw <what> to <who>\n"); |
| 62 | else |
| 63 | write("You throw a "+what+" to "+who+" to get his attention.\n"); |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 64 | |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 65 | sscanf("ab", "%s%s", who, what) |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 66 | ==> result 2, who = "", what = "ab" |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 67 | |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 68 | sscanf("ab", "%s %s", who, what) |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 69 | ==> result 0, who = 0, what = 0 |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 70 | |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 71 | sscanf("ab ", "%s %s", who, what) |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 72 | ==> result 2, who = "ab", what = "" |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 73 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 74 | HISTORY |
| 75 | LDMud 3.3.713/3.2.13 introduced the '+' specifier. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 76 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 77 | SEE ALSO |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 78 | explode(E), regexp(E) |