View Full Version : item lists from text files?
razorboy
07-13-2010, 07:45 PM
I was just wondering if ash has the ability to open text files and pull input from them.
I was thinking of writing an inventory manipulation script, but I don't really want a gigantic list of items in it... plus I would like to reuse the item list.
Is this possible?
heeheehee
07-13-2010, 07:52 PM
file_to_map() (http://wiki.kolmafia.us/index.php?title=File_to_map) and map_to_file() (http://wiki.kolmafia.us/index.php?title=Map_to_file) are your friends. Read up on Data Structures (http://wiki.kolmafia.us/index.php?title=Data_Structures) if you need to figure out maps.
razorboy
07-13-2010, 07:58 PM
Ahh yes. Maps. They keep popping up. (Why can't we just use arrays?)
(Why can't we just use arrays?)
Those are effectively (same interface) just integer-indexed maps (with optimized implementation).
Unless you want static arrays, where you have to know how many items you are storing even *before* you store the firts one.
razorboy
07-13-2010, 08:18 PM
The big difference is that I know how to use static arrays, while maps are new and scary.
Veracity
07-13-2010, 08:19 PM
You want arrays? ASH has them. An array can be considered to be a map that has integer keys numbered from 0 to <n>.
string [int] MyMap;
string [6] MyArray;
MyMap[5] = "this works";
MyMap[100] = "this too";
MyArray[5] = "this works too";
foreach i, s in MyMap
print( "MyMap[" + i + "] = " + s );
foreach i, s in MyArray
print( "MyArray[" + i + "] = " + s );
int index = 100;
MyArray[index] = "this generates a runtime error";
does this:
> ma.ash
MyMap[5] = this works
MyMap[100] = this too
MyArray[0] =
MyArray[1] =
MyArray[2] =
MyArray[3] =
MyArray[4] =
MyArray[5] = this works too
Array index out of bounds (ma.ash, line 17)
razorboy
07-13-2010, 08:28 PM
Awesome! Something I understand! Thanks Veracity!
Back to the file thing:
If someone has admin access to the wiki, they may want to add to the "file_to_map()" entry that the items in the file need to be tab-separated (at least on Windows). It's not readily apparent in the example, and I tried to use spaces... which didn't work.
That information should definitely be added.
If someone has admin access to the wiki, they may want to add to the "file_to_map()" entry that the items in the file need to be tab-separated (at least on Windows). It's not readily apparent in the example, and I tried to use spaces... which didn't work.
Admin access is not necessary. Anyone can add to the wiki. (It's a wiki!) Though it would be nice if you created an account before editing the wiki, just so we know it's you.
Edit: Added info, but perhaps there is a better way of stating it.
philmasterplus
07-14-2010, 03:25 AM
You want arrays? ASH has them. An array can be considered to be a map that has integer keys numbered from 0 to <n>.
string [6] MyArray;
Hmm, never knew that syntax before. That opens a slew of questions:
Do they work with floating-point numbers or strings? (i. e. string [ "stm" ] a_map; string [ 12.5 ] some_map; )
Do they work multidimensionally? (i. e. string [5, 5] multi_dim_array; )
Are they internally treated differently from normal maps? If so, I feel it should be discussed as a separate data type in the wiki.
heeheehee
07-14-2010, 03:55 AM
1. Arrays in this context can only be indexed by integer.
2. Yes, try it out yourself!
ash string [5, 5] multi_dim_array; foreach i,m in multi_dim_array print(i+":"+m)
3. I have no idea, although I do recall JH mentioning that maps, arrays, and datatype constants are the three implementations of aggregates in KoLmafia. Probably a yes?
Powered by vBulletin® Version 4.2.0 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.