Question about maps

Banana Lord

Member
Just a quick question, given the map below, how would I go about retrieving the keys?
Code:
string [string] tester;
tester ["test1"] = "True";
tester ["test2"] = "it's a wombat!";
tester ["test3"] = "42";
tester ["test4"] = "";
I want to do something along these lines:
Code:
foreach test in $strings[??the_keys??]
	print(test)
=>
'test1'
'test2'
'test3'
'test4'
 

matt.chugg

Moderator
You are pretty much there!

Code:
string [string] tester;
tester ["test1"] = "True";
tester ["test2"] = "it's a wombat!";
tester ["test3"] = "42";
tester ["test4"] = "";

foreach test in tester {
	print(test);
}

outputs:

Code:
> call scripts\new2.txt.ash

test1
test2
test3
test4
 

Banana Lord

Member
OK, another one for you.

Say I want to save a list of animals to a data file so that on opening said file I would see this:
Code:
monkey
zebra
But I can't find a satisfactorily simple way of doing it with maps. I could do something like:
Code:
string [string] animals;
animals ["monkey"] = "";
animals ["zebra"] = "";
But that doesn't seem very elegant.
 

Banana Lord

Member
Does file_to_map() return true/false if it succeeds/fails? Based on the code below, I'd assumed that it did not.
Code:
string [string] test_map;
if(!file_to_map("whadjlks.txt", test_map))
	print("File not found");
=> <nothing>
I most certainly do not have a file called "whadjlks.txt".
 
Last edited:

heeheehee

Developer
Staff member
OK, another one for you.

Say I want to save a list of animals to a data file so that on opening said file I would see this:
Code:
monkey
zebra
But I can't find a satisfactorily simple way of doing it with maps. I could do something like:
Code:
string [string] animals;
animals ["monkey"] = "";
animals ["zebra"] = "";
But that doesn't seem very elegant.

You could attempt file_to_map($strings[monkey, zebra], "animals.txt"), but that doesn't quite return what you want (it actually maps those to true). On that note, the nice thing about plural typed constants is that they retain the iteration order that you list them in. So $ints[1, 4, 3, 2] will be iterated over in that order exactly.

Also, regarding map_to_file: doesn't look like there's ever any case in which it'd return false. The code looks for some call or whatever to return null, but it seems like it'd actually return DataUtilities.EMPTY_STREAM in the event that the file doesn't actually exist. The current workaround seems to be "call file_to_map and store the data in some temporary map. If that map's count is greater than 0, return false."
 

Veracity

Developer
Staff member
Also, regarding map_to_file: doesn't look like there's ever any case in which it'd return false. The code looks for some call or whatever to return null, but it seems like it'd actually return DataUtilities.EMPTY_STREAM in the event that the file doesn't actually exist.
That should be fixed. It looks like it was a side-effect of the new DataFileCache feature from a couple of months ago.
 

StDoodle

Minion
EDIT: While saved for idio-posterity, I really should "lrn2r33dn00b", 'specially at 4am after drinking.

As to the original question, what's wrong with:
Code:
boolean [string] animals = $strings[monkey, zebra, rhino];
foreach creature in animals {
   print(creature);
}
Which should give you:
Code:
monkey
zebra
rhino
 
Last edited:

heeheehee

Developer
Staff member
Can you even directly save plural typed constants in a user-defined map? I think it gives you an error, since those are technically different objects -- IIRC, the plural-typed constants are immutable, while map are mutable.
 

heeheehee

Developer
Staff member
I stand corrected. (yeah, I don't think I actually ever checked this; might've just been some arbitrary assumption based on the iteration being in the order that the items were listed. :p)
 

xKiv

Active member
AFAIK, plural values ($items[t,v,a], $effects[x,u,l], ...) are constant literals, implemented with a different type of underlying java object.
It *might* be possible to assing plural value into a map variable. I would recommend actually trying that in running instance of mafia before giving a definite answer.
But it won't be "just a map". They are immutable. You will get exceptions for trying to change the contents.
 
Top