Help with my Code. Mixture of htmlform.as, and Map_to_file()

icon315

Member
I am trying to make it so that you can add items to a file. this is what i have so far.
PHP:
 int itm;
 
 	string [int] my_items;
	file_to_map( "My_Items.txt" , my_items);
	
	attr("size=4 maxlength=4");
	itm = write_field("", "itemnum", "Item Number: ").to_int();
			write_button("itemnumber", "Add");
		string [int] new_item;
		new_item [(count(my_items) + 1)] = itm ;
			if (map_to_file( new_item , "My_Items.txt" ))
			   print( "Your item were saved successfully." );
			else
			   print( "There was a problem saving your item." );
I got the actual saving works. But as soon as i get to the page it saves int 0. then when i try to submit it overwrites the page.

This is the most reason test. I was trying to fix the overwrite problem.

CODE
PHP:
int itm;
 
 	string [int] old_items;
	file_to_map( "My_Items.txt" , old_items);
	
	attr("size=4 maxlength=4");
	itm = write_field("", "itemnum", "Item Number: ").to_int();
			write_button("itemnumber", "Add");
		string [int] new_item;
		for i from 0 to (count(old_items)) {
		new_item [count(old_items)] = old_items[i];
		}
		new_item [(count(old_items) + 2)] = itm ;
		
			if (map_to_file( new_item , "My_Items.txt" ))
			   print( "Your item were saved successfully." );
			else
			   print( "There was a problem saving your item." );
CLI
Code:
Your item were saved successfully.

Begin index -1 out of bounds (relay_Daily_Info.ash, line 134)

this is what the .txt file before
Code:
0	194
1	1450

and after i ran the script
Code:
2	
4	0

and when i submitted a the number 1565 ( first i reset the file)
Code:
2	
4	1565
 
Last edited:
Looking solely at the first code snippet, new_item [(count(my_items) + 1)] = itm ; should be my_items [count(my_items)] = itm ;. Do away with new_item altogether, and instead use map_to_file(my_items, "My_Items.txt").
 
I suspect
new_item [count(old_items)] = old_items;
(inside the for) should avtually be
new_item = old_items;
so that it doesn't write all new items to one position, incidentally throwing away all but the last.

But what slyz and hee3 said - you don't need new_item, you can just as well extend old_items with
old_items[count(old_items)] = itm ;

(ash arrays are zero based, right? An array with N elements has the last element at array[N-1], so the next one is array[N], not array[N+1] or array[N+2])
 
Back
Top