Map to file: Difference between revisions
imported>Jasonharper m Where did you come up with this stuff? |
imported>Bale mNo edit summary |
||
Line 5: | Line 5: | ||
FunctionPage| | FunctionPage| | ||
name={{#var:name}}| | name={{#var:name}}| | ||
function1={{Function| | function1={{Function| | ||
Line 57: | Line 56: | ||
see_also={{SeeAlso|file_to_map}}| | see_also={{SeeAlso|file_to_map}}| | ||
}} | }} | ||
[[Category:Miscellaneous Functions]] |
Revision as of 05:31, 22 May 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 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.
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["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