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

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="bpinsert"></span>
+
= insert() =
 +
'''Procedure.''' Puts a new entry into a stringlist or an intlist at a given position.
  
−
  '''Function:  '''<i><!--CODE-->insert( sl : &lt;stringlist or intlist&gt;, i : integer, s : string ) ;</i><!--ENDCODE-->
+
  insert(list : stringlist or intlist, i : integer, value : string or integer)
  
−
<!--TERM--> '''sl'''
+
The entries from position <code>i</code> on move up one place. Positions count from 0, so
−
<!--DEFINITION-->         the stringlist or intlist you are inserting to
+
<code>insert(sl, 0, s)</code> puts <code>s</code> first. It works on a list variable and on the
−
<!--TERM--> '''i'''
+
list fields of an extra (<code>ex.names</code>, <code>ex.vals</code>,
−
<!--DEFINITION-->         the index where you want to insert the string
+
<code>u.extra.["name"].names</code>), which is how comm.zon keeps a player's tell history
−
<!--TERM--> '''s'''
+
newest first.
−
<!--DEFINITION-->        the string you want to insert
 
  
−
This function allows you to insert a string in a stringlist or intlist
+
== Parameters ==
−
with out re-writing the entire stringlist or intlist to do it. The
+
{| class="wikitable"
−
following Dil will add a string in order to a stringlist.
+
! Name !! Type !! Meaning
−
'''Example:'''
+
|-
−
<i><!--CODE-->
+
| list || stringlist or intlist || The list to change: a variable or a list field, not a function's result.
−
---~---~---~---~---~---~---~---~---
+
|-
 +
| i || integer || The position the new entry gets.
 +
|-
 +
| value || string or integer || The new entry. An integer put into a stringlist becomes its digits; a string put into an intlist becomes its number, as with <code>atoi()</code>.
 +
|}
  
−
dilbegin stringlist add_in_order (sl:stringlist,s:string);
+
== Notes ==
−
var
+
* A stringlist and an intlist treat a position past the end differently. In a stringlist,
−
   i:integer;
+
   empty strings fill the gap: <code>insert(sl, 5, "x")</code> on an empty list gives six
−
   ln:integer;
+
   entries, the last <code>"x"</code>. In an intlist, a position past the end, or below 0, adds
−
code
+
  nothing; <code>i</code> equal to the length adds at the end.
−
dilend
+
* '''Never give a stringlist a negative position.''' It stops the server. A huge position makes
−
 
+
  it try to build a list that long.
−
---~---~---~---~---~---~---~---~---
+
* When <code>i</code> is a failed value (a field of null), the entry goes in at position 0.
−
 
+
* An unset string inserts nothing.
−
</i><!--ENDCODE-->
 
−
 
 
−
 
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 
−
<span id="bpinsert"></span>
 
−
 
 
−
'''Function:  '''<i><!--CODE-->insert( sl : &lt;stringlist or intlist&gt;, i : integer, s : string ) ;</i><!--ENDCODE-->
 
  
−
<!--TERM--> '''sl'''
+
== Example ==
−
<!--DEFINITION-->        the stringlist or intlist you are inserting to
+
<pre>
−
<!--TERM-->  '''i'''
+
dilbegin add_history(ch : unitptr, s : string);
−
<!--DEFINITION-->        the index where you want to insert the string
+
var
−
<!--TERM-->  '''s'''
+
  ex : extraptr;
−
<!--DEFINITION-->        the string you want to insert
+
code
 +
{
 +
  ex := "$tell_history" in ch.extra;
 +
  if (ex == null)
 +
  {
 +
      addextra(ch.extra, {"$tell_history"}, "history");
 +
      ex := "$tell_history" in ch.extra;
 +
  }
  
−
This function allows you to insert a string in a stringlist or intlist
+
  insert(ex.names, 1, s);
−
with out re-writing the entire stringlist or intlist to do it. The
+
  while (length(ex.names) > 21)
−
following Dil will add a string in order to a stringlist.
+
      remove(ex.names, length(ex.names) - 1);
−
'''Example:'''
+
  return;
−
<i><!--CODE-->
+
}
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
dilbegin stringlist add_in_order (sl:stringlist,s:string);
 
−
var
 
−
  i:integer;
 
−
  ln:integer;
 
−
code
 
 
dilend
 
dilend
 +
</pre>
 +
Keeps the 20 latest tells behind the extra's name, newest first, as comm.zon does.
  
−
---~---~---~---~---~---~---~---~---
+
== See also ==
−
 
+
* [[Manual:DIL Manual/remove()|remove()]]
−
</i><!--ENDCODE-->
+
* [[Manual:DIL Manual/addstring()|addstring()]]
−
 
+
* [[Manual:DIL Manual/substring()|substring()]]
−
 
+
* [[Manual:DIL Manual/length()|length()]]
−
 
+
* [[Manual:DIL Manual/stringlist|stringlist]]
−
---~---~---~---~---~---~---~---~---
+
* [[Manual:DIL Manual/intlist|intlist]]

Latest revision as of 23:20, 14 November 2026

insert()

Procedure. Puts a new entry into a stringlist or an intlist at a given position.

insert(list : stringlist or intlist, i : integer, value : string or integer)

The entries from position i on move up one place. Positions count from 0, so insert(sl, 0, s) puts s first. It works on a list variable and on the list fields of an extra (ex.names, ex.vals, u.extra.["name"].names), which is how comm.zon keeps a player's tell history newest first.

Parameters

Name Type Meaning
list stringlist or intlist The list to change: a variable or a list field, not a function's result.
i integer The position the new entry gets.
value string or integer The new entry. An integer put into a stringlist becomes its digits; a string put into an intlist becomes its number, as with atoi().

Notes

  • A stringlist and an intlist treat a position past the end differently. In a stringlist,
 empty strings fill the gap: insert(sl, 5, "x") on an empty list gives six
 entries, the last "x". In an intlist, a position past the end, or below 0, adds
 nothing; i equal to the length adds at the end.
  • Never give a stringlist a negative position. It stops the server. A huge position makes
 it try to build a list that long.
  • When i is a failed value (a field of null), the entry goes in at position 0.
  • An unset string inserts nothing.

Example

dilbegin add_history(ch : unitptr, s : string);
var
   ex : extraptr;
code
{
   ex := "$tell_history" in ch.extra;
   if (ex == null)
   {
      addextra(ch.extra, {"$tell_history"}, "history");
      ex := "$tell_history" in ch.extra;
   }

   insert(ex.names, 1, s);
   while (length(ex.names) > 21)
      remove(ex.names, length(ex.names) - 1);
   return;
}
dilend

Keeps the 20 latest tells behind the extra's name, newest first, as comm.zon does.

See also