Map to file: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
mNo edit summary
imported>StDoodle
mNo edit summary
Line 30: Line 30:


function_description=Saves the data in the {{pspan|map_to_save}} from to the {{pspan|file_to_save}} 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 operation's success (if {{pspan|condition}} is supplied, and evaluates to false, this function returns true). 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>
function_description=Saves the data in the {{pspan|map_to_save}} from to the {{pspan|file_to_save}} 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 operation's success (if {{pspan|condition}} is supplied, and evaluates to false, this function returns true). 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>As this & [[file_to_map|file_to_map()]] are KoLmafia's only I/O functions, they are invaluable for an ASH scripter wishing to save any script settings or information that are 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.|
<p>As this & [[file_to_map|file_to_map()]] are KoLmafia's only I/O functions, they 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 saves 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["Kittens"] = user_confirm( "Do you like kittens?" );
   print( "At index: " + i + " We find: " + my_list[i] );
answers["KoL"] = 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>
<pre>
0 StDoodle
Chocolate false
1 Grotfang
Kittens true
</pre>
KoL true
Then the results would be:
<pre>
At index: 0 We find: StDoodle
At index: 1 We find: Grotfang
</pre>
</pre>
}}|
}}|

Revision as of 01:48, 11 March 2010

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

  • file_to_save is the filename to save to
  • map_to_save is the map to save data from
  • condition is an (optional) condition that must be met for the function to act

Saves the data in the map_to_save from to the file_to_save 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 operation's success (if condition is supplied, and evaluates to false, this function returns true). 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.

As this & file_to_map() are KoLmafia's only I/O functions, they 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["Kittens"] = user_confirm( "Do you like kittens?" );
answers["KoL"] = 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
Kittens	true
KoL	true

See Also

file_to_map()