Feature to_string() or equivalent for aggregates

StDoodle

Minion
What I'd like to request is the ability to call a function that turns an aggregate into exactly the same text that would normally be written to a file using map_to_file(), but as a string.

This could be useful for quickly displaying file data in a relay info script (inside of a textarea, perchance) or for providing content for automated file uploading.

Thank you for your consideration.
 

Alhifar

Member
I think this should do what you want.

The one problem that I ran into that I'm sure needs to be changed is how to display a newline that will print correctly in the CLI. I used "<br>" but I think that to_string() shouldn't add any information to the data.

EDIT: Crap. Tacking "<br>" on the end seems to have made things worse. I'm honestly not sure what should be done with that, so I'll leave the patch as is for now.
 

Attachments

  • aggregatetostring.diff
    811 bytes · Views: 23
Last edited:
I used "<br>" but I think that to_string() shouldn't add any information to the data.

You're not exactly adding data, are you? Just translating from \n to <br>?
Seems safe to me.
Or you could leave it as \n and leave the display method up to the scripter.

Either way, thank you for this, I hope to see it implemented.
EDIT: I'm not totally jive with Java, so the code is slighty iffy to me... does this handle aggregates with multiple dimensions?
 
Last edited:

StDoodle

Minion
Well, if the final string is going to be submitted to a server script, having any html tags would kinda defeat the purpose of what I'm looking for. I'd really rather leave it to the user to replace line breaks / tabs with html.
 

StDoodle

Minion
Sorry to bump, but, well... *bump*. Clan management has become an even more desperate problem now that we have Crimbo content in the basement, and this feature would allow me to script said work. Please, with a cherry? And a lime?
 

heeheehee

Developer
Staff member
Ehh. If foreach could take an arbitrary map (i.e. one passed it by a function), then the following would, in theory, work:
Code:
string to_string(aggregate map) {
    string str;
    foreach i in map {
        str += to_string(i)+"\t"+to_string(map[i]);
    }
    return str;
}

Except instead I get "Aggregate reference expected". I dunno if this counts as a bug (improperly implemented "aggregate" type) or a feature request (add support for this wonky bit of code).

Edit: Also haven't really bothered to get line breaks working. This'd presumably work, though...
Code:
string stringify(string str) {
    return str+"\n";
}
string stringify(aggregate map) {
    string str;
    foreach i in map {
        str += stringify(i)+"\t"+stringify(map[i]);
    }
    return str;
}
 
Last edited:

StDoodle

Minion
You can't declare simply an aggregate; you have to declare, say, "int [string]" or "boolean [int][string]" etc. In other words, every map format would require its own to_string() function, as of now.
 

heeheehee

Developer
Staff member
*Technically* you can -- see ZLib's load_current_map. It's just not compatible with foreach, which is what I was noting.
 

heeheehee

Developer
Staff member
To be honest, since it's not compatible with foreach, there's not really much point to it other than for use with native functions that take an aggregate as an argument: namely, clear(), count(), and map_to_file().
 

Veracity

Developer
Staff member
I don't see how it can be made compatible with foreach. There would be no way for us to know the data types of the keys or values and therefore allow you to use them in the body of the foreach.
 

StDoodle

Minion
That's what I was thinking, especially since some aggregates can be "deeper" and require mutliple foreach's or multiple key references... ugh. Seems like a nightmare, but obviously file_to_map() has something that gets the job done... and access to that would be awesome-spect-fabulo-tastic.
 

Veracity

Developer
Staff member
Seems like a nightmare, but obviously file_to_map() has something that gets the job done... and access to that would be awesome-spect-fabulo-tastic.
file-to-map is a built-in function. It knows that it can take any aggregate and is able to look at the data type of whatever it is passed and decode it. The issue with foreach is that we bind variables to the key and optionally the value and those variables are available in a block of user-written code. You can do arithmetic, data type conversion, whatever on the key and value - and we enforce data types at compile time, not run time. It would be a huge kludge or a fundamental change (with a substantial performance penalty, probably) in how ASH compiles blocks to allow run-time type checking rather than compile time type checking. And it's better for the script author to catch errors at compile time rather than at run time.
 

heeheehee

Developer
Staff member
Fair enough. It'd probably be a lot cleaner to just implement to_string() for aggregates; I was just messing around to see if there was any way to create a function in ASH that'd produce the desired functionality.
 

slyz

Developer
Why not do something like this:
PHP:
string print_file = "temp.txt" ;

string map_to_string()
{
	string str;
	string[ string ] print_map;
	file_to_map( print_file, print_map );
	foreach str1, str2 in print_map str += str1 + " \\t" + str2 + "\\n";
	return str;
}

int [ location ] my_map;
my_map[ $location[ Giant's Castle ] ] = 5 ; 
my_map[ $location[ Outskirts of The Knob ] ] = 10 ; 
my_map[ $location[ Hobopolis Town Square ] ] = 20 ; 

map_to_file( my_map, print_file );
print(map_to_string());

I don't really know what you wanted to use this for, but you can replace "\\t" and "\\n" by either "\t" and "\n", or by "<br>" or whatever suits you.
 

Alhifar

Member
The problem is that it doesn't properly go through all the dimensions in the map to print them all. That's the same problem I'm having with writing a patch. Well, that and that I'm extremely inexperienced with java.
 
Top