file_to_map

Pazleysox

Member
I haven't started coding this yet, but is it possible to pull a specific line from a map?

For example:
text.txt contains this data:
0 (date/time)
1 Council of Loathing
2 Madame Zatara
3 IotM
4 (my clan_name)

I want to be able to access lines 3, & 4.

Is this even possible?
 

ckb

Minion
Staff member
PHP:
	string [int] textmap;
	file_to_map("text.txt",textmap);
	print(textmap[3]);
	print(textmap[4]);
 

Pazleysox

Member
Is there a way I can tell the map_to_file to skip the first 2, or 3 words that it's going to return?

For Example:
For example:
text.txt contains this data:
0 This is not a message
1 Council of Loathing said "Are you ready to crawl into the science donation vessel and perform your final service to the community?"
2 Madame Zatara
3 The current IotM is: (this is what it is)

line 1, I only want "Are you ready to crawl into the science donation vessel and perform your final service to the community?"
line 3, I only want (this is what it is)

Is that possible?
 

Veracity

Developer
Staff member
No. That function returns whatever is in your file. You have two choices:

1) Make sure the data in the file is in the form you want it to be
2) Write code in your script to process what is in the file into the form you want it to be
 

Pazleysox

Member
I found a this (now modified) code in the forum
Code:
void main() {

    matcher m;
string raidlog = visit_url("shop.php?whichshop=generalstore");
	m = create_matcher("was a ([a-z]*)", raidlog);
    if (find(m)) {
        print ("test");
        print(group(m,1));
    }
which produces this:
> call scripts\00TEST.ash

test
hero
Code:
void main() {

    matcher m;
string raidlog = visit_url("shop.php?whichshop=generalstore");
	m = create_matcher("was a (\\w*)", raidlog);
    if (find(m)) {
        print ("test");
        print(group(m,1));
    }
produces the same thing. Is there a regex command to pull ALL the following text? I tried various things like this:
(\\w*) (\\w*), and [a-z]* [a-z]* [a-z]*
but I got the same output each time.

I would like it to say this "hero of the Cola Wars. Now I wage war"
 

heeheehee

Developer
Staff member
" " isn't matched by \w or [a-z]+. Similarly, "." isn't matched by any of those groups.

I'd probably use something like [^<]+ to match everything until the start of the next HTML tag.

edit: also, note that m.group(1) will only return the first capturing group, i.e. what's in the first set of parentheses.
 

Pazleysox

Member
" " isn't matched by \w or [a-z]+. Similarly, "." isn't matched by any of those groups.

I'd probably use something like [^<]+ to match everything until the start of the next HTML tag.

edit: also, note that m.group(1) will only return the first capturing group, i.e. what's in the first set of parentheses.

Code:
	m = create_matcher("was ([^<]+)", raidlog);
    if (find(m)) {
        print ("test");
	print(m);
        print(group(m,1));
Got this to work. Here's my results:

test
was ([^<]+)
a hero of the Cola Wars. Now I wage war against high prices!

Now, how can I put that into a map?
 
Last edited:

Pazleysox

Member
I've tried these, but I keep coming up with errors...

map_to_file(group(m,1), "test.txt");
map_to_file((m,1), "test.txt");
map_to_file(m, "test.txt");
map_to_file(group(m), "test.txt");

string savetofile = group(m,1);

map_to_file(savetofile, "test.txt");
 

AlbinoRhino

Active member
map_to_file saves a map to a file not a string. Create a map such as...int[string] mymap. Fill the map with the strings you want in the file. Then use map_to_file(mymap,"file.txt") to save your map of strings to the file.
 
Last edited:

Pazleysox

Member
map_to_file saves a map to a file not a string. Create a map such as...int[string] mymap. Fill the map with the strings you want in the file. Then use map_to_file(mymap,"file.txt") to save your map of strings to the file.

So, I need to turn the matcher, or (M) into a string?
 

AlbinoRhino

Active member
Code:
     matcher m = create_matcher("was ([^<]+)", raidlog);
[COLOR=#ff0000][B]    string[int] mymap;  <--- define a map to store the string(s)
[/B][/COLOR]
    
   /*
     " if (find(m)) " will find the first match to the pattern defined with create_matcher()
     " [B]while (find(m))[/B] " will keep looking through the 'raidlog' text and find all matches to the pattern.


   */


   if (find(m))
   {
        print ("test");
        print(m);
        print(group(m,1));
       [COLOR=#ff0000][B] mymap[count(mymap)] = group(m,1);  <--- put it in a map -- once with [/B][/COLOR][COLOR=#0000cd][B]if(find(m))[/B][/COLOR][COLOR=#ff0000][B] -OR- repeatedly with [/B][/COLOR][COLOR=#0000ff][B]while(find(m))[/B][/COLOR]
   }

   [COLOR=#ff0000][B]map_to_file(mymap,"myfile.txt");  <--- after done building map, save map to a file[/B][/COLOR]

Now, how can I put that into a map?
 
Last edited:

Pazleysox

Member
record note
{
string message;
int time_;
}
[int] test;

PHP:
    		int NE= count(test);
		test[NE].message= message;
		test[NE].time_ = to_int(now_to_string("HHmm"));
		Map_to_File(test,"pazsox.txt");
	}

pazsox.txt says: 0 This is a test of the emergency broadcasting system 1815

PHP:
	{
	string [int] my_list;
		file_to_map("pazsox.txt", my_list);
		for i from 0 to (count(my_list) -1) {
		print(my_list[i];
		}

Here's my output: This is a test of the emergency broadcasting system

I would like the time displayed also. I'm not sure how to do this...
 

fronobulax

Developer
Staff member
I'm not sure it is correct to expect print to display all fields.

perhaps print(my_list.message + ":" + my_list/time_);

I would expect

This is a test of the emergency broadcasting system:1815

(colon used for illustrative purposes)
 

heeheehee

Developer
Staff member
Presumably you meant to use the record you defined, e.g. as part of the definition of my_list?
PHP:
note [int] my_list;
 

Pazleysox

Member
I'm sure he did. Duh.

I'm only using the record note; because I saw it in another script. I didn't know I didn't need it. Here's my issue:

pazsox.txt says: 0 This is a test of the emergency broadcasting system 1815
PHP:
    {
    string [int] my_list;
        file_to_map("pazsox.txt", my_list);
        for i from 0 to (count(my_list) -1) {
        print(my_list[i];
        }

Here's my output: This is a test of the emergency broadcasting system

I would like the time displayed also. I'm not sure how to do this...

Even if I change how the data is mapped:

test [NE].time_ = to_int(now_to_string("HHmm"));
test [NE].message = message;
vs

test [NE].message = message;
test [NE].time_ = to_int(now_to_string("HHmm"));

my pazsox.txt file comes out with the time in the end.
 
Last edited:

fronobulax

Developer
Staff member
I'm not sure it is easy to understand but you might consider reading http://wiki.kolmafia.us/index.php?title=Data_Structures and see if you understand the differences between a map and a record and a map that is made up of records.

In ash scripting it is common to define a record type, declare an map of that record type and then use the map in file_to_map and map_to_file calls. Sometimes you don't need a record but sometimes you do.

To your specific questions...

If the line in the data file is 0[tab]This is a test of the emergency broadcasting system[tab]1815[eol] where tab and eol are the tab character and your system's end of line character the map to read and write this would be

record note
{
string message;
string time;
}

note[int] myNotes;

and to print the message and time you would use myNotes.message and myNotes.time in your print statement. if the first number in the sample line actually matters to you then you probably need a different map

If the time was always digits in the file you could declare time to be an int in the record.

If you want time to not be the end of the line then you have to find the map that is being used to write the file and change the associated record.
 
Top