Files and maps and arrays, oh my!

nworbetan

Member
I started on a spading project not too long ago, and found myself writing some code that's just silly:
PHP:
if (provisional_muscle != floor((barehanded ? .25 : 1) * my_buffedstat($stat[muscle])) || my_adventures() == 0 || error) {
        string [string, string, string, string, string, string, string, string, string, string, string,string] tmp;
        int i;
        repeat {
                i += 1;
                file_to_map("mc_punchalot-" + provisional_muscle + "-" + i + ".csv", tmp);
        } until (count(tmp) == 0);
        for y from 0  to y_max {
                tmp [data[0,y], data[1,y], data[2,y], data[3,y], data[4,y], data[5,y], data[6,y], data[7,y], data[8,y], data[9,y], data[10,y], data[11,y]] = data[12,y];
        }
        map_to_file(tmp, "mc_punchalot-" + provisional_muscle + "-" + i + ".csv");
        print("Your " + provisional_muscle + " muscle data is waiting for you in mc_punchalot-" + provisional_muscle + "-" + i + ".csv");
        for x from 0 to x_max for y from 0 to y_max data[x,y] = "";
        if (error) abort();
}
I'll give you two guesses which two lines make me feel a bit funny, and you better get both of them right. :p

Seriously though, I'm probably going to end up splitting that one file I'm outputting into a separate file for each monster, because the strings could really get out of control if I were spading a zone with 5 monsters and using 6 columns per monster. (I had to add a .txt to upload the file, it looks much better if you open it with a spreadsheet.)

But to get to the point, would anyone else get any use out of array_to_file() and file_to_array() ASH functions? So far the map versions have done their job good enough to get by, and they still do, but now that I'm wanting to output data with a little more variety, array versions of those two functions would really hit the spot. I wanted to ask here before jumping straight to a feature request though, in case anyone knew of a more elegant way to get array data into a file.
 

Attachments

  • mc_punchalot-346-1.csv.txt
    812 bytes · Views: 41

jasonharper

Developer
An ASH array is just a specialized form of map - better performance, less flexibility. map_to_file() should already work on arrays, file_to_map() would work except that you'd have to somehow know the exact number of elements in advance. I'm not sure what you're trying to accomplish, anyway. The proper way to store data about an arbitrary number of monsters would be to define a record type that holds all the data about a single monster, and put those in a map or array.
 

nworbetan

Member
An ASH array is just a specialized form of map - better performance, less flexibility. map_to_file() should already work on arrays, file_to_map() would work except that you'd have to somehow know the exact number of elements in advance. I'm not sure what you're trying to accomplish, anyway. The proper way to store data about an arbitrary number of monsters would be to define a record type that holds all the data about a single monster, and put those in a map or array.

Okay, the point about using a record is well taken, and there's a pretty good chance that I'll be able to use that to make the collection process simpler. But what I'm trying to accomplish is is collecting a large quantity of data that's both human and computer readable. Specifically with the array_to_file() and file_to_array() I'm wanting to use them to do the simplest and most straightforward 2 dimensional array <--> tab seperated file conversion possible.

I just ran:
Code:
ash int i = 0; int[3,3]a; for x from 0 to 2 for y from 0 to 2 { i += 1; a[x,y] = i; } map_to_file(a, "test.txt");
and the contents of the test.txt file is not at all what I'm aiming for. It's nice to know that that does work, and that might be exactly what I want to do with some other script but formatting the data is more important to me right now than writing pithy code.
 
Last edited:

jasonharper

Developer
So, with your desired array_to_file() you'd expect output like this?
1 <tab> 2 <tab> 3
4 <tab> 5 <tab> 6
7 <tab> 8 <tab> 9

* What would you expect it to do with an array of more than two dimensions?
* What would you expect it to do if the array elements were themselves maps (or otherwise consist of a variable number of elements)?
 

nworbetan

Member
Yes, that's the output that I've jumped through a couple hoops to produce, and if it were packaged into an easier to use function I'd be happy. I could be the only one though, file output is a pretty niche activity.

I hadn't considered any situations more complicated than the one I'm working with, but I'm glad you asked anyway. The one concrete expectation I have with respect to those compounded examples is that result of file_to_array() should reconstruct the array that was fed into array_to_file() to begin with. And then I ran:
Code:
ash int[3,3]a; file_to_map("test.txt", a); for x from 0 to 2 for y from 0 to 2 print(a[x,y]);
and was pretty happy with the results. :)

So, now I'm wondering what would happen if I use a map of records as an argument to map_to_file(). I'm assuming that would restore the record to it's original condition, hopefully? I don't have any experience with them other than the one I stole from Rinn for the Beer Pong adventure.
 

StDoodle

Minion
Trust me, my clan management scripts use file_to_map() and vice-versa on maps of records that contain maps of records etc. etc., and they go both ways just fine. The only caveat I've seen as of yet is that if you get into anything that "wonky," you'll need to escape any plain tab and newline characters first, if you don't want to break things.
 

nworbetan

Member
Is your clan management script something I can find on these forums if I look? I'm curious to see a practical application of maps of records of maps of records.

Also, thanks for helping me look at my situation from a couple different points of view. Simply switching from a string[19,12] to a string[monster,7,12] made my script substantially more powerful/flexible by removing the static limit of concurrent monsters tracked, noticeably simplified the formatting code that didn't get axed, and shortened it by nearly 30 lines.

Also also:
PHP:
if (provisional_muscle != floor((barehanded ? .25 : 1) * my_buffedstat($stat[muscle])) || my_adventures() == 0 || error) {
    foreach mon in data {
// TODO replace spaces in mon.to_string() with underscores
        string monname = mon.to_string().to_lowercase();
        string [string] tmp;
        int i;
        repeat {
            i += 1;
            file_to_map("mc_punchalot-" + monname + "-"  + provisional_muscle + "-" + i + ".csv", tmp);
        } until (count(tmp) == 0);
        for y from 0 to y_max {
            if (x_max > 0) {
                string foo;
                for x from 0 to x_max {
                    if (x < x_max) foo += data[mon,x,y] + "\t";
                    else tmp[foo] = data[mon,x,y];
                }
            } else tmp[data[mon,0,y]] = "";
        }
        map_to_file(tmp, "mc_punchalot-" + monname + "-" + provisional_muscle + "-" + i + ".csv");
        print("Your data is waiting for you in mc_punchalot-" + monname + "-" + provisional_muscle + "-" + i + ".txt");
    }
    if (error) abort();
    foreach m in data for x from 0 to x_max for y from 0 to y_max data[m,x,y] = "";
}
It's untested, but if it doesn't quite work yet, it will with minor adjustments.
 
Last edited:
Top