## ## table_as_string() ## ## Purpose: This gets a table (header and data) in a string, convenient for dumping/debugging. ## ## Parameters: t: the table ## returns the table as a string ## subroutine(table_as_string(table t), ( # debug_message("\n\n#### table_as_string() START \n"); string table_string; node header = get_table_header(t); int numRows = table_get_num_rows(t); int numColumns = num_subnodes(header); # Compute the width of the header columns node columnWidth = new_node(); int columnnum = 0; node headerNode; foreach headerNode header ( if (length(@headerNode{'field_name'}) > @columnWidth{columnnum}) then @columnWidth{columnnum} = length(@headerNode{'field_name'}); columnnum++; ); # Compute the width of the data columns string cellvalue; for (int rownum = 0; rownum < numRows; rownum++) ( for (int columnnum = 0; columnnum < numColumns; columnnum++) ( cellvalue = table_get_cell_string_value(t, rownum, columnnum); if (length(cellvalue) > @columnWidth{columnnum}) then @columnWidth{columnnum} = length(cellvalue); ); # for columnnum ); # for rownum #node columnWidthNode; #columnnum = 0; #foreach columnWidthNode columnWidth ( # echo("Width of column " . columnnum . ": " . @columnWidthNode); ## @columnWidthNode += 2; # columnnum++; #); # Print the header string headerLine; bool first = true; string cellvalue; columnnum = 0; foreach headerNode header ( if (first) then headerLine .= '| '; cellvalue = @headerNode{'field_name'}; while (length(cellvalue) < @columnWidth{columnnum}) cellvalue .= ' '; headerLine .= cellvalue; headerLine .= ' | '; columnnum++; first = false; ); table_string .= headerLine . "\n"; # Print the data in the table string dataLine = ''; #echo("numRows=" . numRows); #echo("numColumns=" . numColumns); string cellvalue; for (int rownum = 0; rownum < numRows; rownum++) ( dataLine = ''; first = true; for (int columnnum = 0; columnnum < numColumns; columnnum++) ( if (first) then dataLine .= '| '; # if (length(dataLine) > 0) then # dataLine .= ","; #echo("rownum=" . rownum . ", columnnum=" . columnnum); cellvalue = table_get_cell_string_value(t, rownum, columnnum); while (length(cellvalue) < @columnWidth{columnnum}) cellvalue .= ' '; dataLine .= cellvalue; dataLine .= ' | '; first = false; # if (length(cellvalue) > @columnWidth{columnnum}) then # @columnWidth{columnnum} = length(cellvalue); ); table_string .= dataLine . "\n"; ); # for rownum table_string; # debug_message("\n\n#### table_as_string() END \n"); )); # table_as_string()