Difference between revisions of "File to map"

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
m
imported>StDoodle
Line 1: Line 1:
'''[[boolean]] file_to_map( [[string]] file_to_load , [[aggregate]] map_to_fill )'''
+
{{
 +
#vardefine:name|file_to_map}}{{
 +
#vardefine:return_type|boolean}}{{
  
'''[[boolean]] file_to_map( [[string]] file_to_load , [[aggregate]] map_to_fill , [[boolean]] condition_for_loading )'''
+
FunctionPage|
 +
name={{#var:name}}|
 +
function_category=Miscellaneous|
  
Loads a map from a saved file, and returns a [[boolean]] of the operation's success. While all that is required to execute file_to_map() is the saved file's name and a compatible map, it can also contain a conditional that must evaluate as true to execute the file-map load.
+
function1={{Function|
 +
name={{#var:name}}|
 +
aggregate={{#var:aggregate}}|
 +
return_type={{#var:return_type}}|
 +
return_also={{#var:return_also}}|
 +
parameter1={{Param|string|file_to_load}}|
 +
parameter2={{Param|aggregate|map_to_fill}}|
 +
}}|
  
If file_to_map() is called from within a script, it will automatically load the specified filename from the KoLMafia\Scripts\ directory ([[map_to_file()]]'s automatic in-script save location). If file_to_map() is called from within a library function, it will load the specified filename from the KoLMafia\Data\ directory (the save location of [[map_to_file()]] when run within a library function).
+
function2={{Function|
 +
name={{#var:name}}|
 +
aggregate={{#var:aggregate}}|
 +
return_type={{#var:return_type}}|
 +
return_also={{#var:return_also}}|
 +
parameter1={{Param|string|file_to_load}}|
 +
parameter2={{Param|aggregate|map_to_fill}}|
 +
parameter3={{Param|boolean|condition}}|
 +
p1desc={{Pspan|file_to_load}} is the filename to load from|
 +
p2desc={{Pspan|map_to_fill}} is the map to populate with data|
 +
p3desc={{Pspan|condition}} is an (optional) condition that must be met for the function to act|
 +
}}|
  
 +
function_description=Loads data to the {{pspan|map_to_fill}} from a saved {{pspan|file_to_load}} in your KoLmafia "data" directory. If {{pspan|condition}} is supplied, it must evalute to true in order for the function to proceed. This function returns the operations success (if {{pspan|condition}} is supplied, and evaluates to false, this function returns true). If {{pspan|map_to_fill}} has not been initialized, this function will abort (as oppossed to returning false).|
  
<p>''A script example of loading a pre-defined map from a file:''<pre>#Loading (and displaying) an arbitrary map from a file
+
code1={{CodeSample|
string[int] favorite_things;
+
title=Code Sample|
 
+
description=This sample loads a simple map that includes item names keyed by a number.|
if( file_to_map( "ponies_and_candycanes.txt" , favorite_things ) == true )
+
code=
{
+
<syntaxhighlight>
   print( "All of your favorite things have come here to KoLMafia to play! Yay! There's:" , "#FF6666" );
+
string [int] my_list;
  for cycle from 1 upto (count(favorite_things)-1)
+
file_to_map( "SavedList.txt" , my_list);
  {
+
foreach name in my_list {
      print( ""+favorite_things[cycle]+", and..." , "#FF00CC" );
+
   print( "We have a name: " , my_list[name] );
  }
 
  print( ""+favorite_things[count(favorite_things)]+", oh my!" , "#FF00CC" );
 
 
}
 
}
else
+
</syntaxhighlight>|
{
+
moreinfo=
  print( "Your favorite things failed to load. Apparently they don't love you back." , "Red" );
+
If the file "SavedList.txt" had the following:
}</pre></p>
+
<pre>
<p>Would obviously produce a different output depending on what you had saved in the list. For example, were the contents of "ponies_and_candycanes.txt":''<pre>1      Sunshine
+
StDoodle 0
2      Cabbage
+
Grotfang 1
3      Computers
+
</pre>
4      The Sound of Music
+
Then the results would be:
5      Paddington Bear</pre></p>
+
blargh
 +
}}|
  
<p>Would yield an output of:<pre>All of your favorite things have come here to KoLMafia to play! Yay! There's:
+
see_also={{SeeAlso|map_to_file|}}|
Sunshine, and...
+
}}
Cabbage, and...
 
Computers, and...
 
The Sound of Music, and...
 
Paddington Bear, oh my!</pre></p>
 
{{Format}}
 

Revision as of 00:55, 11 March 2010

Function Syntax

boolean file_to_map(string file_to_load ,aggregate map_to_fill )

boolean file_to_map(string file_to_load ,aggregate map_to_fill ,boolean condition )

  • file_to_load is the filename to load from
  • map_to_fill is the map to populate with data
  • condition is an (optional) condition that must be met for the function to act

Loads data to the map_to_fill from a saved file_to_load in your KoLmafia "data" directory. If condition is supplied, it must evalute to true in order for the function to proceed. This function returns the operations success (if condition is supplied, and evaluates to false, this function returns true). If map_to_fill has not been initialized, this function will abort (as oppossed to returning false).

Code Sample

This sample loads a simple map that includes item names keyed by a number.

string [int] my_list;
file_to_map( "SavedList.txt" , my_list);
foreach name in my_list {
   print( "We have a name: " , my_list[name] );
}

If the file "SavedList.txt" had the following:

StDoodle	0
Grotfang	1

Then the results would be:

blargh

See Also

map_to_file()