Difference between revisions of "Map to file"

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
(Not the only I/O functions anymore)
 
(5 intermediate revisions by 3 users not shown)
Line 5: Line 5:
 
FunctionPage|
 
FunctionPage|
 
name={{#var:name}}|
 
name={{#var:name}}|
function_category=Miscellaneous|
 
  
 
function1={{Function|
 
function1={{Function|
Line 23: Line 22:
 
parameter1={{Param|aggregate|map_to_save}}|
 
parameter1={{Param|aggregate|map_to_save}}|
 
parameter2={{Param|string|file_to_save}}|
 
parameter2={{Param|string|file_to_save}}|
parameter3={{Param|boolean|condition}}|
+
parameter3={{Param|boolean|compact}}|
p1desc={{Pspan|file_to_save}} is the filename to save to|
+
p1desc={{Pspan|map_to_save}} is the map to save data from|
p2desc={{Pspan|map_to_save}} is the map to save data from|
+
p2desc={{Pspan|file_to_save}} is the filename to save to|
p3desc={{Pspan|condition}} is an (optional) condition that must be met for the function to act|
+
p3desc={{Pspan|compact}} is an (optional) flag; if omitted or true, record values are written on a single line of the file when possible, rather than one field per line|
 
}}|
 
}}|
  
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).|
+
function_description=Saves the data in the {{pspan|map_to_save}} to the {{pspan|file_to_save}} in your KoLmafia "data" directory. The {{pspan|compact}} parameter exists only for backwards compatibility; there is no need to use it in new code (but if you do use it, you must pass the same value to [[file_to_map|file_to_map()]] to successfully read any records within your map). This function returns the operation's success. If {{pspan|file_to_save}} already exists, this function will override (not append) the file's contents with the new data. It's also worth noting that, once saved, the data in {{pspan|file_to_save}} is not tied to any particular map; it can be loaded into another map or slice, as long as the datatypes are compatible.</p>
 +
 
 +
<p>This function and <code>[[file_to_map|file_to_map()]]</code> are invaluable for an ASH scripter wishing to save any script settings or information too complex to easily fit in a user preference. However, caution should be used, as a single failed operation of this function could result in the loss of all such data. For anything difficult to re-create, or too important to lose, the script author should be careful to save backups and check for function success during such operations.|
  
 
code1={{CodeSample|
 
code1={{CodeSample|
 
title=Code Sample|
 
title=Code Sample|
description=This sample loads a simple map that includes item names keyed by a number.|
+
description=This sample saves a simple map of a user's responses to questions.|
 
code=
 
code=
 
<syntaxhighlight>
 
<syntaxhighlight>
string [int] my_list;
+
boolean [string] answers;
file_to_map( "SavedList.txt" , my_list);
+
answers["Chocolate"] = user_confirm( "Do you like chocolate?" );
for i from 0 to (count(my_list) - 1) {
+
answers["CuteKittens"] = user_confirm( "Do you like cute kittens?" );
   print( "At index: " + i + " We find: " + my_list[i] );
+
answers["ThisGame"] = user_confirm( "Do you like KoL?" );
}
+
if (map_to_file( answers , "WhatYouLike.txt" ))
 +
   print( "Your answers were saved successfully." );
 +
else
 +
  print( "There was a problem saving your answers." );
 
</syntaxhighlight>|
 
</syntaxhighlight>|
 
moreinfo=
 
moreinfo=
If the file "SavedList.txt" had the following:
+
If the user answered No, Yes, Yes to the questions, they should now have a file that contains:
<pre>
 
0 StDoodle
 
1 Grotfang
 
</pre>
 
Then the results would be:
 
 
<pre>
 
<pre>
At index: 0 We find: StDoodle
+
Chocolate false
At index: 1 We find: Grotfang
+
CuteKittens true
 +
ThisGame true
 
</pre>
 
</pre>
 
}}|
 
}}|
  
see_also={{SeeAlso|map_to_file|}}|
+
see_also={{SeeAlso|buffer_to_file|file_to_array|file_to_buffer|file_to_map}}|
 
}}
 
}}
 +
 +
[[Category:Miscellaneous Functions]]

Latest revision as of 05:51, 21 December 2020

Function Syntax

boolean map_to_file(aggregate map_to_save ,string file_to_save )

boolean map_to_file(aggregate map_to_save ,string file_to_save ,boolean compact )

  • map_to_save is the map to save data from
  • file_to_save is the filename to save to
  • compact is an (optional) flag; if omitted or true, record values are written on a single line of the file when possible, rather than one field per line

Saves the data in the map_to_save to the file_to_save in your KoLmafia "data" directory. The compact parameter exists only for backwards compatibility; there is no need to use it in new code (but if you do use it, you must pass the same value to file_to_map() to successfully read any records within your map). This function returns the operation's success. If file_to_save already exists, this function will override (not append) the file's contents with the new data. It's also worth noting that, once saved, the data in file_to_save is not tied to any particular map; it can be loaded into another map or slice, as long as the datatypes are compatible.

This function and file_to_map() are invaluable for an ASH scripter wishing to save any script settings or information too complex to easily fit in a user preference. However, caution should be used, as a single failed operation of this function could result in the loss of all such data. For anything difficult to re-create, or too important to lose, the script author should be careful to save backups and check for function success during such operations.

Code Sample

This sample saves a simple map of a user's responses to questions.

boolean [string] answers;
answers["Chocolate"] = user_confirm( "Do you like chocolate?" );
answers["CuteKittens"] = user_confirm( "Do you like cute kittens?" );
answers["ThisGame"] = user_confirm( "Do you like KoL?" );
if (map_to_file( answers , "WhatYouLike.txt" ))
   print( "Your answers were saved successfully." );
else
   print( "There was a problem saving your answers." );

If the user answered No, Yes, Yes to the questions, they should now have a file that contains:

Chocolate	false
CuteKittens	true
ThisGame	true

See Also

buffer_to_file() | file_to_array() | file_to_buffer() | file_to_map()