Difference between revisions of "Manual:DIL Manual/getword()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="bfgword"></span>
+
= getword() =
−
string getword ( var s : string )
+
'''Function''' (string). Takes the first word off a string.
−
    s : A string with zero or more words separated by space.
 
−
    return: The first word of the string.
 
  
−
    NB: The argument string has the returned word removed.
+
getword(s : string)
  
−
---~---~---~---~---~---~---~---~---
+
Returns the first word of <code>s</code>. When <code>s</code> 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 <code>argument</code> apart.
−
<span id="bfgword"></span>
 
−
string getword ( var s : string )
 
−
    s : A string with zero or more words separated by space.
 
−
    return: The first word of the string.
 
  
−
    NB: The argument string has the returned word removed.
+
== Parameters ==
 +
{| class="wikitable"
 +
! Name !! Type !! Meaning
 +
|-
 +
| s || string || The text. Its first word is cut off if it is a variable (or <code>argument</code>).
 +
|}
  
−
---~---~---~---~---~---~---~---~---
+
== Returns ==
 +
String: the first word, with its case kept. "" when <code>s</code> is empty or only spaces. It fails if <code>s</code> fails or is a string variable that has never had a value.
 +
 
 +
== Notes ==
 +
* Only a string variable, <code>argument</code>, or an assignable string field such as <code>pc.guild</code> or <code>pc.hometown</code> is shortened (the field really changes). An expression such as <code>getword(s + "")</code> leaves <code>s</code> alone.
 +
* On <code>argument</code> the change lasts until the program waits again; after that, <code>argument</code> 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 <code>while (s != "") w := getword(s);</code> never ends on such a string. Use <code>getwords()</code> or <code>split()</code> for text that may hold line breaks.
 +
* It is a function: <code>getword(s);</code> on its own line is a syntax error. Write <code>w := getword(s);</code>.
 +
* <code>getwords()</code> returns all the words at once without changing the string.
 +
 
 +
== Example ==
 +
<pre>
 +
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
 +
</pre>
 +
Takes the first word of "pull lever hard"; <code>rest</code> is then "hard".
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/getwords()|getwords()]]
 +
* [[Manual:DIL Manual/split()|split()]]
 +
* [[Manual:DIL Manual/argument|argument]]
 +
* [[Manual:DIL Manual/left()|left()]]
 +
* [[Manual:DIL Manual/mid()|mid()]]

Latest revision as of 23:15, 14 November 2026

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