File to map: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
No edit summary
 
(23 intermediate revisions by 9 users not shown)
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}}|


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|compact}}|
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|compact}} is an (optional) flag; if omitted or true, records that do not contain any aggregate values are expected to be written on a single line, rather that one line per field.|
}}|


function_description=<p>Loads data to the {{pspan|map_to_fill}} from a saved {{pspan|file_to_load}} in your KoLmafia "data" or "scripts" directory. Any data originally stored in the {{pspan|map_to_fill}} are wiped before loading the file. The {{pspan|compact}} parameter is not needed unless you specified it in the <code>[[map_to_file|map_to_file()]]</code> call that originally generated the file, in which case the values must match.</p>


<p>''A script example of loading a pre-defined map from a file:''<pre>#Loading (and displaying) an arbitrary map from a file
<p>This function returns <code>false</code> if the data loaded from {{pspan|file_to_load}} is invalid. Note that this function is ''extremely'' forgiving. If {{pspan|file_to_load}} does not exist, it will behave as though it is loading an empty file, and return <code>true</code>. If the file's contents do not match {{pspan|map_to_fill}}, it will still load it into {{pspan|map_to_fill}} on a best-effort basis and return <code>true</code>.</p>
string[int] favorite_things;


if( file_to_map( "ponies_and_candycanes.txt" , favorite_things ) == true )
<p>You can also directly load internal data files used by KoLmafia, such as concoctions.txt and fullness.txt, by specifying their name&#151;KoLmafia will automatically load the file for you. For a full list of internal data files used by KoLmafia, see the [http://sourceforge.net/p/kolmafia/code/HEAD/tree/src/data/ KoLmafia source repository].|
{
 
  print( "All of your favorite things have come here to KoLMafia to play! Yay! There's:" , "#FF6666" );
code1={{CodeSample|
  for cycle from 1 upto (count(favorite_things)-1)
title=Code Sample|
   {
description=This sample loads a simple map that includes item names keyed by a number.|
      print( ""+favorite_things[cycle]+", and..." , "#FF00CC" );
code=
  }
<syntaxhighlight lang="d">
  print( ""+favorite_things[count(favorite_things)]+", oh my!" , "#FF00CC" );
string [int] my_list;
file_to_map( "SavedList.txt" , my_list);
for i from 0 to (count(my_list) - 1) {
   print( "At index: " + i + " We find: " + my_list[i] );
}
}
else
</syntaxhighlight>
{
}}
  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
0 StDoodle
2      Cabbage
1 Grotfang
3      Computers
</pre>
4      The Sound of Music
Then the results would be:
5      Paddington Bear</pre></p>
<pre>
At index: 0 We find: StDoodle
At index: 1 We find: Grotfang
</pre>
{{CodeSample|
description=The following sample loads the full list of {{kolwiki|Category:Spleentacular Items|spleen-damaging items}} from [http://kolmafia.svn.sourceforge.net/viewvc/kolmafia/src/data/spleenhit.txt spleenhit.txt].|
code=<syntaxhighlight lang="d">
int [item] spleen_hits;
file_to_map( "spleenhit.txt" , spleen_hits );
 
foreach itm, spleen_hit in spleen_hits
  print( itm + " will damage " + spleen_hit + " of your spleen." );
</syntaxhighlight>|
}}|
 
see_also={{SeeAlso|buffer_to_file|file_to_array|file_to_buffer|map_to_file}}|
more_info=You can load any information stored in a data file, as long as your map contains the appropriate [[Data Types]] separated by tabs. Spaces are just considered to be part of a string.<br />
Any line that begins with a number sign (<code>#</code>) is considered a comment, and is ignored by KoLmafia.|
}}


<p>Would yield an output of:<pre>All of your favorite things have come here to KoLMafia to play! Yay! There's:
[[Category:Miscellaneous Functions]]
Sunshine, and...
Cabbage, and...
Computers, and...
The Sound of Music, and...
Paddington Bear, oh my!</pre></p>

Latest revision as of 05:44, 10 March 2021

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 compact )

  • file_to_load is the filename to load from
  • map_to_fill is the map to populate with data
  • compact is an (optional) flag; if omitted or true, records that do not contain any aggregate values are expected to be written on a single line, rather that one line per field.

Loads data to the map_to_fill from a saved file_to_load in your KoLmafia "data" or "scripts" directory. Any data originally stored in the map_to_fill are wiped before loading the file. The compact parameter is not needed unless you specified it in the map_to_file() call that originally generated the file, in which case the values must match.

This function returns false if the data loaded from file_to_load is invalid. Note that this function is extremely forgiving. If file_to_load does not exist, it will behave as though it is loading an empty file, and return true. If the file's contents do not match map_to_fill, it will still load it into map_to_fill on a best-effort basis and return true.

You can also directly load internal data files used by KoLmafia, such as concoctions.txt and fullness.txt, by specifying their name&#151;KoLmafia will automatically load the file for you. For a full list of internal data files used by KoLmafia, see the KoLmafia source repository.

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);
for i from 0 to (count(my_list) - 1) {
   print( "At index: " + i + " We find: " + my_list[i] );
}

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

0	StDoodle
1	Grotfang

Then the results would be:

At index: 0 We find: StDoodle
At index: 1 We find: Grotfang

The following sample loads the full list of spleen-damaging items from spleenhit.txt.

int [item] spleen_hits;
file_to_map( "spleenhit.txt" , spleen_hits );

foreach itm, spleen_hit in spleen_hits
   print( itm + " will damage " + spleen_hit + " of your spleen." );

See Also

buffer_to_file() | file_to_array() | file_to_buffer() | map_to_file()

More Information

You can load any information stored in a data file, as long as your map contains the appropriate Data Types separated by tabs. Spaces are just considered to be part of a string.
Any line that begins with a number sign (#) is considered a comment, and is ignored by KoLmafia.