Difference between pages "String Handling Routines" and "Operators"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>Spiny Twizzler
 
imported>Bale
(this badly needs fixing up, but at least the data is here and off my to-do list)
 
Line 1: Line 1:
== Part  8 - String Handling Routines ==
+
{{TOCright}}
<p><em>Under Construction</em></p>
+
==Mathematical Operators==
<p><strong>void print( string helloworld )</strong><br>
 
Prints the given string to the graphical CLI window and to the status line of the main Adventuring window.</p>
 
<p><strong>string [[visit_url alpha|visit_url( string url )]]</strong><br>
 
Accesses the specified URL, manages any applicable redirects (including if the page offered a choice adventure), and returns the HTML of the final response page.</p>
 
<p><strong>string location_to_url( location place )</strong><br>
 
  
Returns the URL that would need to be accessed to visit the specified location.</p>
+
The following mathematical operators are used in KoLmafia:
<p><strong>boolean contains_text( string source, string query )</strong><br>
+
{{
Reveals if the query string is a substring of the source string.</p>
+
eztable|
<p><strong>int extract_meat( string text )</strong><br>
+
{{eztr|{{eztd| + |Addition|Performs addition and string concatenation}}}}
Returns the amount of meat contained in a string passed to it in integer format. For use in k-mail parsing. *Not for use on pending trades.*</p>
+
{{eztr|{{eztd| - |Subtraction|Performs subtraction}}}}
<p><strong>int [item] extract_items( string text )</strong><br>
+
{{eztr|{{eztd| * |Multiplication|Performs multiplication}}}}
takes the text you have provided and parses it for any items that KoLmafia would have found normally.  This means you have access to the built-in pluralization handler as well as the ability to NOT have to lookup description IDs or whatever other loops you had to use to access that information. For use in k-mail parsing. *Not for use on pending trades.*</p>
+
{{eztr|{{eztd| / |Division|Performs division}}}}
<p><strong>int length( string text )</strong><br>
+
{{eztr|{{eztd| % |Modulo|Returns the remainder after division}}}}
Returns the length of the given string.</p>
+
{{eztr|{{eztd| ^ |Exponent|Performs exponentiation}}}}
 +
}}
 +
Note that, with the exception of using <nowiki>"+"</nowiki> for string concatenation, these operators can only be used on int or float datatypes.
  
<p><strong>int index_of( string source, string search )</strong><br>
+
==Assignment Operators==
Returns the first index of a given substring in a string.<br>
+
The following assignment operators are used in KoLmafia (let a = left operand, b = right operand):
<strong>int index_of( string source, string search, int start )</strong><br>
+
{{
Returns the next index of a given substring in a string starting from the given position.<br>
+
eztable|
<strong>int last_index_of( string source, string search )</strong><br>
+
{{eztr|{{eztd| <nowiki>=</nowiki> |<nowiki>a = b</nowiki>}}}}
Returns the last index of a given substring in a string.</p>
+
{{eztr|{{eztd| <nowiki>+=</nowiki> |<nowiki>a = a + b</nowiki>}}}}
<p><strong>string substring( string source, int startfrom )</strong><br>
+
{{eztr|{{eztd| <nowiki>-=</nowiki> |<nowiki>a = a - b</nowiki>}}}}
Returns the substring of the given string starting from the given character position in the string, and ending at the last character.<br>
+
{{eztr|{{eztd| <nowiki>*=</nowiki> |<nowiki>a = a * b</nowiki>}}}}
<strong>string substring( string source, int start, int end )</strong><br>
+
{{eztr|{{eztd| <nowiki>/=</nowiki> |<nowiki>a = a / b</nowiki>}}}}
 +
{{eztr|{{eztd| <nowiki>%=</nowiki> |<nowiki>a = a % b</nowiki>}}}}
 +
}}
 +
Of these, only += and = are usable for strings.  See [[Operators#Mathematical Operators|Mathematical Operators]] for information regarding the basic Mathematical Operators.
  
Returns the substring of the given string starting from the character position indicated by start in the string, and ending at the character position indicated by end.</p>
+
==Relational Operators==
<p><strong>string replace_string( string source, string search, string replace )</strong><br>
 
searches the source string for the search string and replaces all instances with the replace string</p>
 
<p><strong>string url_encode( string text )</strong><br>
 
<strong>string url_decode( string text )</strong><br>
 
Converts a string text into a formatted string for using with raw URLs or from a formatted string back into “normal” text.</p>
 
  
<p><strong>string [int] split_string( string source )</strong><br>
+
To follow these examples, a basic understanding of the concepts found on [[Control Structures]] would be helpful.
Returns a map of strings which is the passed string split into individual lines. Originally implemented for use when parsing session logs, but may see more uses in the future.
 
http://kolmafia.us/index.php/topic,794.msg3868.html#msg3868 <br>
 
<strong>string [int] split_string( string source, string regex )</strong><br>
 
<strong>string [int,int] group_string( string source, string regex )</strong><br>
 
see this post located on the script repository for more information:  http://kolmafia.us/index.php/topic,451.msg2235.html#msg2235 </p>
 
  
<strong>string [int] session_logs( string player, int day_count )</strong>
+
In order to create more complex if statements, we need to understand the basic relational operators:
Gives access to the session logs saved by kolmafia. More details will be added when this function's own page is written. For now, more extensive details can be found here: http://kolmafia.us/showthread.php?t=573
+
{{
 +
eztable|
 +
{{eztr|{{eztd| <nowiki>==</nowiki> |equal to}}}}
 +
{{eztr|{{eztd| <nowiki>!=</nowiki> |not equal to}}}}
 +
{{eztr|{{eztd| <nowiki><</nowiki> |less than}}}}
 +
{{eztr|{{eztd| <nowiki>></nowiki> |greater than}}}}
 +
{{eztr|{{eztd| <nowiki><=</nowiki> |less than or equal to}}}}
 +
{{eztr|{{eztd| <nowiki>>=</nowiki> |greater than or equal to}}}}
 +
}}
 +
Note that you cannot mix datatypes within a comparison or KoLmafia will abort with an error, with the exception of mixing types int and float, where KoLmafia will do a transparent type conversion behind-the-scenes. If you need to compare different datatypes, use one or more of the [[Datatype Conversions|Datatype Conversion]] functions. Also, == is case-insensitive with respect to strings.
  
<p><strong>buffer append( buffer source, string text )</strong><br>
+
{{
Returns the source buffer with the string added on at the end.<br>
+
CodeSample|
<strong>buffer append_tail( matcher regex, buffer source )</strong><br>
+
code=
This function appends the text returned by a matcher to the end of the buffer text.
+
<syntaxhighlight>
 +
if ( true == true )
 +
{
 +
  print( "This line DOES get printed." );
 +
}
 +
if ( true == false )
 +
{
 +
  print( "This line does NOT get printed." );
 +
}
 +
if ( 1 == 1.0 )
 +
{
 +
  print( "This line DOES get printed." );
 +
}
 +
if ( 1 == 2 )
 +
{
 +
  print( "This line does NOT get printed." );
 +
}
 +
if ( "Hello" == "hello" )
 +
{
 +
  print( "This line DOES get printed." );
 +
}
 +
</syntaxhighlight>}}
 +
==Boolean Operators==
 +
{{
 +
eztable|
 +
{{eztr|{{eztd| <nowiki>&&</nowiki> |and}}}}
 +
{{eztr|{{eztd| <nowiki>||</nowiki> |or}}}}
 +
{{eztr|{{eztd| <nowiki>!</nowiki> |not}}}}
 +
}}
 +
Note that the above operators only work with boolean values & datatypes. To make use of them with other datatypes, you will either need to first perform a [[Datatype Conversions|Datatype Conversion]], or you will need to nest your operations such that a boolean value is used with the boolean operators.
 +
{{
 +
CodeSample|
 +
code=
 +
<syntaxhighlight>
 +
if ( true && true )
 +
{
 +
  print( "This line DOES get printed (both possibilities proved true)." );
 +
}
 +
if ( true && false )
 +
{
 +
  print( "This line does NOT get printed (only one possibility proved true)." );
 +
}
 +
if ( true || false )
 +
{
 +
  print( "This line DOES get printed (since at least one of the possibilities proved true)." );
 +
}
 +
if ( ! false )
 +
{
 +
  print( "This line DOES get printed (since the not operator converted false to true)." );
 +
}
 +
</syntaxhighlight>}}
 +
We also need to understand operator precedence. Statements inside a () pair are always evaluated first, then from left to right.
 +
{{
 +
CodeSample|
 +
code=
 +
<syntaxhighlight>
 +
if ( true || true && false )
 +
{
 +
  print( "This line DOES get printed." );
 +
  // evaluated left-to-right
 +
  // true or (true && false) returns true
 +
}
 +
if ( true && ( true && false ) )
 +
{
 +
  print( "This line does NOT get printed." );
 +
  // ( true && false ) is evaluated first since it is inside of parentheses
 +
  // so we end up evaluating ( true && false ) which returns false
 +
}
 +
if ( true && ! ( true && false ) )
 +
{
 +
  print( "This line DOES get printed." );
 +
  // ( true && false ) is evaluated first since it is inside of parentheses
 +
  // the ! operator converts the false from ( true && false ) to true
 +
  // ( true && true ) returns true
 +
}
 +
</syntaxhighlight>}}
 +
 
 +
==Bitwise Operators==
 +
 
 +
The following mathematical operators are used for operating on the bits of integers. The operands must be integers; no coercion is allowed:
 +
{{
 +
eztable|
 +
{{eztr|{{eztd| & |and|a & b}}}}
 +
{{eztr|{{eztd| | |or|a | b}}}}
 +
{{eztr|{{eztd| ~ |not|Performs multiplication}}}}
 +
{{eztr|{{eztd| << |left shift|a << b}}}}
 +
{{eztr|{{eztd| >> |right shift|a >> b}}}}
 +
{{eztr|{{eztd| &= |and|a &= b --> a = a & b}}}}
 +
{{eztr|{{eztd| |= |or|a |= b --> a = a | b}}}}
 +
{{eztr|{{eztd| >>> |unsigned right shift|a >>> b}}}}
 +
{{eztr|{{eztd| >>= |right shift|a >>= b --> a = a >> b}}}}
 +
{{eztr|{{eztd| >>>= |unsigned right shift|a >>>= b -> a = >>> b}}}}
 +
}}
 +
 
 +
[[Category:Scripting]]

Revision as of 22:59, 12 January 2011

Mathematical Operators

The following mathematical operators are used in KoLmafia:

+ AdditionPerforms addition and string concatenation
- SubtractionPerforms subtraction
* MultiplicationPerforms multiplication
/ DivisionPerforms division
 % ModuloReturns the remainder after division
^ ExponentPerforms exponentiation

Note that, with the exception of using "+" for string concatenation, these operators can only be used on int or float datatypes.

Assignment Operators

The following assignment operators are used in KoLmafia (let a = left operand, b = right operand):

= a = b
+= a = a + b
-= a = a - b
*= a = a * b
/= a = a / b
 %= a = a % b

Of these, only += and = are usable for strings. See Mathematical Operators for information regarding the basic Mathematical Operators.

Relational Operators

To follow these examples, a basic understanding of the concepts found on Control Structures would be helpful.

In order to create more complex if statements, we need to understand the basic relational operators:

== equal to
 != not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to

Note that you cannot mix datatypes within a comparison or KoLmafia will abort with an error, with the exception of mixing types int and float, where KoLmafia will do a transparent type conversion behind-the-scenes. If you need to compare different datatypes, use one or more of the Datatype Conversion functions. Also, == is case-insensitive with respect to strings.


if ( true == true )
{
   print( "This line DOES get printed." );
}
if ( true == false )
{
   print( "This line does NOT get printed." );
}
if ( 1 == 1.0 )
{
   print( "This line DOES get printed." );
}
if ( 1 == 2 )
{
   print( "This line does NOT get printed." );
}
if ( "Hello" == "hello" )
{
   print( "This line DOES get printed." );
}

Boolean Operators

&& and
|| or
 ! not

Note that the above operators only work with boolean values & datatypes. To make use of them with other datatypes, you will either need to first perform a Datatype Conversion, or you will need to nest your operations such that a boolean value is used with the boolean operators.

if ( true && true )
{
   print( "This line DOES get printed (both possibilities proved true)." );
}
if ( true && false )
{
   print( "This line does NOT get printed (only one possibility proved true)." );
}
if ( true || false )
{
   print( "This line DOES get printed (since at least one of the possibilities proved true)." );
}
if ( ! false )
{
   print( "This line DOES get printed (since the not operator converted false to true)." );
}

We also need to understand operator precedence. Statements inside a () pair are always evaluated first, then from left to right.

if ( true || true && false )
{
   print( "This line DOES get printed." );
   // evaluated left-to-right
   // true or (true && false) returns true
}
if ( true && ( true && false ) )
{
   print( "This line does NOT get printed." );
   // ( true && false ) is evaluated first since it is inside of parentheses
   // so we end up evaluating ( true && false ) which returns false
}
if ( true && ! ( true && false ) )
{
   print( "This line DOES get printed." );
   // ( true && false ) is evaluated first since it is inside of parentheses
   // the ! operator converts the false from ( true && false ) to true
   // ( true && true ) returns true
}


Bitwise Operators

The following mathematical operators are used for operating on the bits of integers. The operands must be integers; no coercion is allowed:

& anda & b
ora b
~ notPerforms multiplication
<< left shifta << b
>> right shifta >> b
and
ora b
>>> unsigned right shifta >>> b
right shift
unsigned right shift