Difference between pages "ASH Errors" and "Insert"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>Bale
m
 
imported>Heeheehee
m (Missed one. Whoops.)
 
Line 1: Line 1:
{{TOCright}}
+
{{
Errors happen. When they do, it helps to have some idea of what what wrong.
+
#vardefine:name|insert}}{{
 +
#vardefine:return_type|buffer}}{{
  
===Abort===
+
FunctionPage|
 +
name={{#var:name}}|
 +
function_category=String Handling Routines|
  
Whenever a script runs the [[abort|abort()]] function, this error is generated.
+
function1={{Function|
 +
name={{#var:name}}|
 +
aggregate={{#var:aggregate}}|
 +
return_type={{#var:return_type}}|
 +
return_also={{#var:return_also}}|
 +
parameter1={{Param|buffer|original}}|
 +
parameter2={{Param|int|strStart}}|
 +
parameter3={{Param|string|stuff}}|
 +
p1desc={{Pspan|original}} is the buffer to modify|
 +
p2desc={{Pspan|strStart}} marks where to insert the string|
 +
p3desc={{Pspan|stuff}} is the string to insert|
 +
}}|
  
{{CodeSample|
+
function_description=Returns the buffer {{pspan|original}} with the string {{pspan|stuff}} inserted at {{pspan|strStart}} removed.|
description=The zero-parameter form:|
 
code=
 
<syntaxhighlight>
 
abort();
 
</syntaxhighlight>}}
 
will stop the execution and print: <span style="color:red">KoLmafia declares world peace.</span> Note that this message may also appear as a result of pressing Esc in the Main Interface or hitting "stop now" in the Adventure tab.
 
 
 
{{CodeSample|
 
description=The one-parameter form:|
 
code=
 
<syntaxhighlight>
 
abort("Aborting script...");
 
</syntaxhighlight>}}
 
will stop the execution and print: <span style="color:red">Aborting script...</span>
 
 
 
===Cannot return [datatype] from [datatype] function===
 
 
 
This indicates that the return value does not match the function's type.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int stuff(){
 
  return "4";
 
}
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Cannot return string value from int function (test.ash, line 2)</span>
 
 
 
{{CodeSample|
 
description=Similarly, for void functions:|
 
code=
 
<syntaxhighlight>
 
void stuff(){
 
  return 4;
 
}
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Cannot return a value from a void function (test.ash, line 2)</span>
 
 
 
===Encountered '[break|continue]' outside loop===
 
 
 
This indicates that the control structure in question was not in a loop.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
continue;
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Encountered 'break' outside of loop (test.ash, line 1)</span>
 
 
 
===Expected===
 
 
 
This generally indicates a syntax problem (missing ending semi-colon, unmatched braces, unmatched parenthesis etc...).
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int a = 1
 
print(a);
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Expected ;, found print (test.ash, line 2)</span>
 
 
 
==="if" requires a boolean conditional expression ===
 
 
 
This indicates that the condition inside an if statement is not a boolean.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
if(42) {
 
  print("Don't panic.");
 
}
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">"if" requires a boolean conditional expression (test.ash, line 1)</span>
 
 
 
===Index type is not a primitive type===
 
 
 
This indicates that the map's index is not a standard datatype (float, int, string, etc.).
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
record alpha{
 
  int a;
 
  int b;
 
};
 
int[alpha] map;
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Index type 'alpha' is not a primitive type (test.ash, line 5)</span>
 
 
 
===Invalid field name===
 
 
 
This indicates that the record does not contain the field in question.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
record my_record {
 
  string a;
 
  string b;
 
};
 
 
 
my_record [int] my_map;
 
my_map[1].c = "hello";
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Invalid field name 'c' (test.ash, line 7)</span>
 
 
 
Note that this error may also be encountered when neglecting to name a field.
 
{{CodeSample|
 
description= For instance:|
 
code=
 
<syntaxhighlight>
 
record my_record {
 
  string;
 
};
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Invalid field name ';' (test.ash, line 2)</span>
 
 
 
===Invalid type name===
 
 
 
This indicates that the specified type is not recognized by Mafia.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int[fruit] map;
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Invalid type name 'fruit' (test.ash, line 1)</span>
 
 
 
===Main method must appear at top level===
 
 
 
This indicates that the script's main method is inside another method, for some reason. This is usually caused by a missing brace.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
void stuff() {
 
  // stuff happens here
 
 
 
void main() {
 
  stuff();
 
}
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">main method must appear at top level (test.ash, line 6)</span>
 
 
 
===Missing return value===
 
 
 
The last line of a user-defined function has to be "return <value>;" (although the [[Control_Structures#return|return]] command can be used before too).
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int my_function( int a, int b ) {
 
    if ( a > b ) return a ;
 
    else return b ;
 
}
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Missing return value (test.ash, line 4)</span>
 
  
 
+
code1={{CodeSample|
===No closing found===
+
title=Code Sample|
 
+
description=Inserts a "Refresh page" link to the Pyramid inside KoL's default border.|
This can be caused by forgetting to put a '''"''' or a ''']''' at the end of an argument
 
 
 
{{CodeSample|
 
description=Example:|
 
 
code=
 
code=
 
<syntaxhighlight>
 
<syntaxhighlight>
buy(1000 , $item[disco ball, 225);
+
buffer results;
</syntaxhighlight>}}
+
results.append(visit_url());
will cause this error: <span style="color:red">No closing ] found (testing.ash, line 1)</span>
 
  
===Record expected===
+
int strEnd = results.index_of("height=137 border=0>");
 
+
if(strEnd > 0) {
This indicates that an unrecognized record was found. This often appears when you forget to append "()" to a function name.
+
  results.insert(strEnd + 20,"<br /><div align='center' class='tiny'><a href='pyramid.php'>Refresh page</a></div>");
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
print(4.to_string);
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Record expected (test.ash, line 2)</span>
 
 
 
===Record name is already defined===
 
 
 
This indicates that the record name has already been defined, and so cannot be reused. The solution is to change the record name.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
record str {
 
  string a;
 
  string b;
 
};
 
record str {
 
  int a;
 
  int b;
 
};
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Record name 'str' is already defined (test.ash, line 5)</span>
 
 
 
===Return needs [datatype] value===
 
 
 
This indicates that the return value is void when the function is not.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int stuff() {
 
  return;
 
 
}
 
}
</syntaxhighlight>}}
+
results.write();
will cause this error: <span style="color:red">Return needs int value (test.ash, line 2)</span>
+
</syntaxhighlight>
 
+
}}|
===Reserved word cannot be a [function|record|variable] name===
 
 
 
This indicates that the desired name has been [[Reserved Words|reserved]], and so cannot be used. The solution is to use a different name.
 
 
 
{{CodeSample|
 
description=An invalid name:|
 
code=
 
<syntaxhighlight>
 
int float(float a) {
 
  return round(a);
 
}
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Reserved word 'float' cannot be used as a function name (test.ash, line 1)</span>
 
 
 
An interesting point to note is that ASH functions take precedent over identically named custom functions rather than throw an exception.
 
 
 
{{CodeSample|
 
description=An invalid record name:|
 
code=
 
<syntaxhighlight>
 
record string {
 
  string a;
 
  string b;
 
};
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Reserved word 'string' cannot be a record name (test.ash, line 1)</span>
 
 
 
{{CodeSample|
 
description=An invalid variable name:|
 
code=
 
<syntaxhighlight>
 
string item = "bottle of gin";
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Reserved word 'item' cannot be a variable name (test.ash, line 1)</span>
 
 
 
===Script parsing error===
 
 
 
This generally indicates a syntax problem, as in having an excess amount of braces or a variable/function/record name that starts with an invalid character.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
put_shop(0 ,0 ,$item[wolf mask]);
 
put_shop(0 ,0 ,$item[rave whistle]);
 
put_shop(0 ,0 ,$item[giant needle]);
 
cli_execute ("undercut");
 
}
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Script parsing error (test.ash, line 5)</span>
 
 
 
===Variable is already defined===
 
 
 
This indicates that the same variable has been declared twice.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int Drops ;
 
int Meat ;
 
int Drops = numeric_modifier("Item Drop");
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Variable Drops is already defined (test.ash, line 3)</span>
 
 
 
 
 
{{RFI|Obviously, a lot more info is needed here.}}
 
  
[[Category:Scripting]]
+
see_also={{SeeAlso|delete}}|
 +
}}

Revision as of 05:05, 20 April 2010

Function Syntax

buffer insert(buffer original ,int strStart ,string stuff )

  • original is the buffer to modify
  • strStart marks where to insert the string
  • stuff is the string to insert

Returns the buffer original with the string stuff inserted at strStart removed.

Code Sample

Inserts a "Refresh page" link to the Pyramid inside KoL's default border.

buffer results;
results.append(visit_url());

int strEnd = results.index_of("height=137 border=0>");
if(strEnd > 0) {
   results.insert(strEnd + 20,"<br /><div align='center' class='tiny'><a href='pyramid.php'>Refresh page</a></div>");
}
results.write();

See Also

delete()