File to map: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Grotfang
m Whoops. Misread.
No edit summary
Line 28: Line 28:
}}|
}}|


function_description=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 [[map_to_file|map_to_file()]] call that originally generated the file, in which case the values must match. This function returns the operation's success. If {{pspan|map_to_fill}} has not been delcared, this function will abort (as oppossed to returning false).</p>
function_description=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. This function returns the operation's success. If {{pspan|map_to_fill}} has not been delcared, this function will abort (as oppossed to returning false).</p>


<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].|
<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].|
Line 36: Line 36:
description=This sample loads a simple map that includes item names keyed by a number.|
description=This sample loads a simple map that includes item names keyed by a number.|
code=
code=
<syntaxhighlight>
<syntaxhighlight lang="d">
string [int] my_list;
string [int] my_list;
file_to_map( "SavedList.txt" , my_list);
file_to_map( "SavedList.txt" , my_list);
Line 56: Line 56:
{{CodeSample|
{{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].|
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>
code=<syntaxhighlight lang="d">
int [item] spleen_hits;
int [item] spleen_hits;
file_to_map( "spleenhit.txt" , spleen_hits );
file_to_map( "spleenhit.txt" , spleen_hits );
Line 65: Line 65:
}}|
}}|


see_also={{SeeAlso|map_to_file|}}|
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 />
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.|
Any line that begins with a number sign (<code>#</code>) is considered a comment, and is ignored by KoLmafia.|

Revision as of 05:52, 21 December 2020

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 the operation's success. If map_to_fill has not been delcared, this function will abort (as oppossed to returning false).

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.