Need LUA help with tables print in columns

Runes of Magic/Radiant Arcana (http://www.runesofmagic.com)
Post Reply
Message
Author
d003232
Posts: 1252
Joined: Wed Jun 03, 2009 4:27 pm

Need LUA help with tables print in columns

#1 Post by d003232 » Fri Aug 28, 2009 9:01 am

I try since some hours to understand lua tables. And I'm a little frustrated. :-(

What I want to do, is to read the waypoints directory and print out that files in three columns. (goal is to select a path from the list)

Now I have the strange result, that column 1 is empty and column 2 disapers (= nil) and only column 3 works. And in column 3, at the last line there is as a strange content the value "column_nr" ( Thats the name I use as I define the table).

If someone could give me a hand. Just enter that code in bot.lua before line 231

Code: Select all

load_paths(wp_to_load, rp_to_load);	-- load the waypoint path / return path
here is my code:

Code: Select all

-- ********** BEGIN OF NEW STUFF
	-- choose a path from the waypoints folder
	local dir = getDirectory(getExecutionPath() .. "/waypoints/");
	local pathlist = { }
 
	printf("Waypoint files in %s:\n", getExecutionPath());
	  

	-- copy table dir to table pathlist
	-- select only xml files
	local hf_counter = 0;
	for i,v in pairs(dir) do
		if( string.find (v,".xml",1,true) ) then
			hf_counter = hf_counter + 1;
			pathlist[hf_counter] = v;
		end
	end
	
	local hf_max_rows = math.ceil(table.getn(pathlist) / 3 );	-- how many rows to output by 3 column
	hf_print_table = { row = { column = { column_nr = { } }  }  };	-- DEFINE1
	local hf_row = 0;
	local hf_column = 1;	-- start in column 1
	
	-- copy entrys from table pathlist to a new table 'hf_print_table' 
	-- arrange in three columns
	for i,v in pairs(pathlist) do
		hf_row = hf_row + 1;
		if( hf_row > hf_max_rows ) then		-- switch column after maxrow
			hf_column = hf_column + 1;
			hf_row = 1;
		end
	
		hf_print_table[hf_row] = { column = { {} }  };
		hf_print_table[hf_row].column[hf_column] = { }; 
-- I don't understand why I need the two lines above???
-- shouldn't the table be defined through the DEFINE1 line?

		hf_print_table[hf_row].column[hf_column].nr = i; 	-- remember nr of the entry
		hf_print_table[hf_row].column[hf_column].filename = v;	-- waypoint filename
		printf("i %s, row %s, col %s: %s \n", i, hf_row, hf_column, hf_print_table[hf_row].column[hf_column].filename);
	end

	printf("tentrys %s\n", table.getn(hf_print_table) );	-- number of entries in table

	-- printout the table with the columns
	for i,v in pairs(hf_print_table) do
		printf("test row %s \n", i );
		local line = "";
--		if( v.column[1].filename ~= nil ) then 
--			line = v.column[1].nr..": "..v.column[1].filename; 
--		end

--		if( v.column[2].filename ~= nil ) then 
--			line = line .. v.column[2].nr..": "..v.column[2].filename; 
--		end
		for h,cols in pairs(v.column) do
		     	printf("test column %s \n", h );
-- problem here:
-- column 1 is empty
-- column 2 is nill
-- only column 3 has the right values, but also at the end there are strange values: string "column_nr"

			if( cols.filename ~= nil ) then 
				printf("row %s column %s  filenr %s %s\n", i, h, cols.nr, cols.filename );
			end

--		if( v.column[3].filename ) then 
--			line = line .. v.column[3].nr..": "..v.column[3].filename; 
--		end

		end
	end
	
	printf("Enter the number of the path you want to use:\n");
	-- later, if the table works :-(

-- ********** END OF NEW STUFF

	load_paths(wp_to_load, rp_to_load);	-- load the waypoint path / return path
	
	-- special option for use waypoint file from profile in a reverse order / not if forced path
	if( settings.profile.options.WAYPOINTS_REVERSE == true  and
	    not forcedPath  ) then 
		__WPL:reverse();
	end;
The RoM Bot Online Wiki needs your help!

d003232
Posts: 1252
Joined: Wed Jun 03, 2009 4:27 pm

Re: Need LUA help with tables print in columns

#2 Post by d003232 » Fri Aug 28, 2009 11:52 am

Code: Select all

hf_print_table[hf_row] = { column = { {} }  };
      hf_print_table[hf_row].column[hf_column] = { }; 
I seems to come from the initialisation, which deletes the old values. I have the feeling I now come closer and will find a working solution.
The RoM Bot Online Wiki needs your help!

User avatar
Administrator
Site Admin
Posts: 5329
Joined: Sat Jan 05, 2008 4:21 pm

Re: Need LUA help with tables print in columns

#3 Post by Administrator » Fri Aug 28, 2009 3:12 pm

That's a pretty big mess. Exactly what are the 3 different values per row you are trying to store in the table?

d003232
Posts: 1252
Joined: Wed Jun 03, 2009 4:27 pm

Re: Need LUA help with tables print in columns

#4 Post by d003232 » Sat Aug 29, 2009 1:55 am

Administrator wrote:That's a pretty big mess. Exactly what are the 3 different values per row you are trying to store in the table?
A line will look like:

Code: Select all

001: file1.xml               006: filexdf.xml               012: waypoint.xml
Is there e elegant way to print ' 1' (leading spaces) instead of '001'?

I did now the table to print in more easy. Means not a table in a table, in a table, ...

And after doing that, I get it to work. My fault was really the initialization of the tabel structure. And up to now, I don't understand when and how I have to initializate a table(see DEFINE 1 & DEFINE 2). I do it be try and error :-(:

Code: Select all

		-- choose a path from the waypoints folder
		local dir = getDirectory(getExecutionPath() .. "/waypoints/");
		local pathlist = { }

		printf("Waypoint files in %s:\n", getExecutionPath());

		-- copy table dir to table pathlist
		-- select only xml files
		local hf_counter = 0;
		for i,v in pairs(dir) do
			if( string.find (v,".xml",1,true) ) then
				hf_counter = hf_counter + 1;
				pathlist[hf_counter] = v;
			end
		end

		local hf_max_rows = math.ceil(table.getn(pathlist) / 3 );	-- how many rows to output by 3 column
		hf_print_table = { row = {   }  };	-- DEFINE1
		local hf_row = 0;
		local hf_column = 1;	-- start in column 1

		-- copy entrys from table pathlist to a new table 'hf_print_table' 
		-- arrange in three columns
		for i,v in pairs(pathlist) do
			hf_row = hf_row + 1;
			if( hf_row > hf_max_rows ) then		-- switch column after maxrow
				hf_column = hf_column + 1;
				hf_row = 1;
			end

			if( not hf_print_table[hf_row] ) then
				hf_print_table[hf_row] = {  };          -- DEFINE 2
				hf_print_table[hf_row] = { column = { {} }  };   -- DEFINE 2
			end

			if( hf_column == 1 ) then
				hf_print_table[hf_row].col1_nr = sprintf("%.3d", i); 	-- remember nr of the entry
				hf_print_table[hf_row].col1_filename = string.sub(v.."                    ", 1, 20);	-- waypoint filename
			elseif( hf_column == 2 ) then
				hf_print_table[hf_row].col2_nr = sprintf("%.3d", i); 	-- remember nr of the entry
				hf_print_table[hf_row].col2_filename = string.sub(v.."                    ", 1, 20);	-- waypoint filename
			elseif( hf_column == 3 ) then
				hf_print_table[hf_row].col3_nr = sprintf("%.3d", i); 	-- remember nr of the entry
				hf_print_table[hf_row].col3_filename = string.sub(v.."                    ", 1, 20);	-- waypoint filename
			end
		end

		-- printout the table with the columns
		for i,v in pairs(hf_print_table) do
			local line = "";
			if( v.col1_nr ~= nil ) then 
				line = v.col1_nr..": "..v.col1_filename; 
			end

			if( v.col2_nr ~= nil ) then 
				line = line.."  "..v.col2_nr..": "..v.col2_filename; 
			end

			if( v.col3_nr ~= nil ) then 
				line = line.."  "..v.col3_nr..": "..v.col3_filename; 
			end

			cprintf(cli.green, "%s\n", line );
		end
The RoM Bot Online Wiki needs your help!

User avatar
Administrator
Site Admin
Posts: 5329
Joined: Sat Jan 05, 2008 4:21 pm

Re: Need LUA help with tables print in columns

#5 Post by Administrator » Sat Aug 29, 2009 4:50 am

Well, really, the easiest way to do it would be to keep it in a simple array and just make use of the information better.

Here's a demonstration on how to print a table in this format:

Code: Select all

[value1]     [value5]     [value9]
[value2]     [value6]     [value10]
etc...

Code: Select all

	local tab = {
		"value1",
		"value2",
		"value3",
		"value4",
		"value5",
		"value6",
		"value7",
		"value8",
		"value9",
		"value10",
	};


	local inc = math.ceil(#tab/3)
	for i = 1,inc do
		local val1 = tab[i];
		local val2 = tab[i + inc];
		local val3 = tab[i + inc*2];

		if( not val1 ) then val1 = ""; end;
		if( not val2 ) then val2 = ""; end;
		if( not val3 ) then val3 = ""; end;

		printf("[%s]\t[%s]\t[%s]\n", val1, val2, val3);
	end

And if you want:

Code: Select all

[value1]     [value2]     [value3]
[value4]     [value5]     [value6]

Code: Select all

for i = 1,#tab, 3 do
  local val1, val2, val3;
  val1 = tab[i];
  val2 = tab[i + 1];
  val3 = tab[i + 2];

  if( not val1 ) then val1 = ""; end;
  if( not val2 ) then val2 = ""; end;
  if( not val3 ) then val3 = ""; end;

  printf("[%s]\t[%s]\t[%s]\n", val1, val2, val3);
end

d003232
Posts: 1252
Joined: Wed Jun 03, 2009 4:27 pm

Re: Need LUA help with tables print in columns

#6 Post by d003232 » Sat Aug 29, 2009 6:09 am

Administrator wrote:Well, really, the easiest way to do it would be to keep it in a simple array and just make use of the information better.
Thx. Thats a good tipp. I will do it like you suggest with one table lesser and use the table index. I just commit my version this morning and will change it soon.
The RoM Bot Online Wiki needs your help!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 0 guests