Manual:DIL Manual/getword()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:15, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

getword()

Function (string). Takes the first word off a string.

getword(s : string)

Returns the first word of s. When s is a string variable, the word and the spaces after it are also removed from that variable, so repeated calls walk through the words one by one. This is the usual way to take a command's argument apart.

Parameters

Name Type Meaning
s string The text. Its first word is cut off if it is a variable (or argument).

Returns

String: the first word, with its case kept. "" when s is empty or only spaces. It fails if s fails or is a string variable that has never had a value.

Notes

  • Only a string variable, argument, or an assignable string field such as pc.guild or pc.hometown is shortened (the field really changes). An expression such as getword(s + "") leaves s alone.
  • On argument the change lasts until the program waits again; after that, argument belongs to the next event anyway.
  • Leading spaces are skipped; a word ends at the first space, tab, line break or other control character. Only spaces are skipped, though: a string that starts with a tab or a line break gives "" and stays as it is, so a loop like while (s != "") w := getword(s); never ends on such a string. Use getwords() or split() for text that may hold line breaks.
  • It is a function: getword(s); on its own line is a syntax error. Write w := getword(s);.
  • getwords() returns all the words at once without changing the string.

Example

dilbegin puller();
var
   rest : string;
   what : string;
code
{
:start:
   wait(SFB_CMD, command("pull") and (activator.type == UNIT_ST_PC));
   rest := argument;
   what := getword(rest);
   if (what == "")
   {
      act("Pull what?", A_ALWAYS, activator, null, null, TO_CHAR);
      goto start;
   }
   if (not (what in {"lever", "handle"}))
      goto start;
   block;
   act("You pull the lever. (Rest of the line: '" + rest + "')",
       A_ALWAYS, activator, null, null, TO_CHAR);
   goto start;
}
dilend

Takes the first word of "pull lever hard"; rest is then "hard".

See also