Saving Stuff to a text file

Winterbay

Active member
You can use file_to_map to load a text file into an ash-map and then use map_to_file to export it again, but you will need ot know beforehand what the data in the text file looks like.
 

Des80

Member
Ok I'll have to look more into the file to map and the map to files.. The example at the bottom of the wiki still leaves some questions but I'll figure them out. I was wanting to Input the amount of adventures certain things take and put into a text File without relying on going through the sessions seperatly and working off of a print() to print text I have to look through in the sessions.
 

Des80

Member
Ok I can copy it to a File now... Im trying to figure out the way to take the number I put into the file and bring it back into the script.

testrun["Number "] = testnumber;

map_to_file( testrun , "thisisatest.txt" );

Thats what I have for the map to file so if I put the testnumber = 3 it will put number 3 in thisisatest.txt

What code snippet would I have to do to pull that number back out of the file and plug it in another int variable.

Edit: I can pull the Slice from the text file but how does one pull the key ( if i wanted to pull the "Number" and 3 ); Also how does one go about making multiple keys and slices ? Just curious
 
Last edited:

Paragon

Member
file_to_map("thisisatest.txt",testrun)


You can add more fields by using a map to a record, or by having a map with multidimentional keys (Is that the right terminology?)
i.e bool [int, string]map

foreach k, v in map
{
/* iterates a bunch of bool [string] maps with an int key */
}


Also
ashwiki file_to_map
ashwiki map_to_file
 
Last edited:

Bale

Minion
Here's an example you might find more helpful.

PHP:
# First create a map of values. 
int [string] ordinal;
ordinal ["First"]  = 1;
ordinal ["Second"] = 2;
ordinal ["Third"]  = 3;
ordinal ["Fourth"] = 4;

# Let's write that information to a text file
map_to_file( ordinal , "test.txt" );

# We now have a file whose first value is First, Second, Third, Fourth followed by a tab and then a number.
# Note that the file is in alphabetical order of the index! After each index is the value assigned to that index.
# If you care about the order of the lines, then I'd suggest that you index them with integers instead of strings.

# Now let's read that information into a map called reord
int [string] reord;
file_to_map( "test.txt", reord );

# Print it out!
foreach key in reord
    print( key + " is " + reord[key] );

I hope that is clearer?
 

Des80

Member
Very Much so on the part about calling the keys. Is there a way to call specific keys or specific slices if you use multiple keys or slices ?
 

xKiv

Active member
Very Much so on the part about calling the keys. Is there a way to call specific keys or specific slices if you use multiple keys or slices ?

Not in general, if that is what I think it is.

Code:
int [string, string] test;

void main() {
        test['a','b']   = 1;
        test['a','c']   = 2;
        test['b','b']   = 3;

        print(test['a','b']); # prints 1
        print(test['a']); # prints aggregate int [string]

        int [string] test2;
        test2 = test['a'];
        print(test2['b']);
}

So you can get "rollup" slices (first key, first two keys, first three keys), but I don't think there's a way to ask for any other slices (like test[*,'b'] "would be" {a->1, b->3})
 

Des80

Member
I love when I try to find a solution to a question and come up with a lot more information more then what I needed but information that intrigued and interested you and made you want to learn more. Thank you everyone for the help.
 

Soluzar

Member
When I do this with a script I've been trying to write, Mafia prepends a line number to every entry in my map. Is there a way to stop this?
 
Top