diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index 767abdd..f6e90df 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -36,15 +36,16 @@ int main(int argc, char const *argv[]) try tc.delimeter('|'); tc.head_number(1); tc.load_text("tmp/world_data", ".txt", ColHead|RowHead); - tc.info(AttInfo|HeadInfo|TagInfo|ColInfo); + tc.info(AttInfo|HeadInfo|TagInfo); tc.filter("America", "Continent_s", ColHead); - tc.save_text("out"); + //tc.filter("America", "BLZ", RowHead); + //tc.save_text("out"); dsv_io tc2 = tc.export_table(); - tc2.head_records(tc.head_records()); + //tc2.head_records(tc.head_records()); tc2.delimeter('|'); - tc2.save_text("out2"); + tc2.save_text("out"); /* geodsv_io tc; diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index c02de32..50f7e80 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -530,7 +530,7 @@ namespace gctl * * @param cnd_str 正则表达式 * @param cnd_tar 用于匹配正则表达式的行或列名称 - * @param thead 用于匹配正则表达式的行或列类型 RowHead时表示按列过滤,ColHead时表示按行过滤 + * @param thead 用于匹配正则表达式的行或列类型 RowHead表示名称为行头则按列过滤,ColHead时表示名称为列头则按行过滤 */ void filter(std::string cnd_str, std::string cnd_tar, table_headtype_e thead = RowHead); @@ -569,8 +569,8 @@ namespace gctl * 因为没有使用strtk库的相关内容,所以并不支持对字符串与数字类型的混合条件判断。基于字符串的内容提取请使用其他函数。 * * @param cnd_str 条件表达式 - * @param cnd_tars 用于匹配正则表达式的行或列名称 - * @param thead 用于匹配正则表达式的行或列类型 RowHead时表示按列过滤,ColHead时表示按行过滤 + * @param cnd_tars 用于计算条件表达式的行或列名称数组 + * @param thead 用于计算条件表达式的行或列类型 RowHead表示名称为行头则按列过滤,ColHead时表示名称为列头则按行过滤 */ void filter(std::string cnd_str, const std::vector &cnd_tars, table_headtype_e thead = RowHead); diff --git a/tool/dsviewer/dsviewer.cpp b/tool/dsviewer/dsviewer.cpp index 7410e1b..d623767 100644 --- a/tool/dsviewer/dsviewer.cpp +++ b/tool/dsviewer/dsviewer.cpp @@ -93,7 +93,7 @@ char *command_generator(const char *text, int state) * contents of rl_line_buffer in case we want to do some simple * parsing. Return the array of matches, or NULL if there aren't any. */ -char **gridmanager_completion(const char *text, int start, int end) +char **dsviewer_completion(const char *text, int start, int end) { char **matches = nullptr; @@ -108,10 +108,10 @@ char **gridmanager_completion(const char *text, int start, int end) void initialize_readline(void) { /* Allow conditional parsing of the ~/.inputrc file. */ - rl_readline_name = "gridmanager"; + rl_readline_name = "dsviewer"; /* Tell the completer that we want a crack first. */ - rl_attempted_completion_function = gridmanager_completion; + rl_attempted_completion_function = dsviewer_completion; } } // End C section @@ -126,7 +126,7 @@ void display_cmds() std::clog << "\nEnter \"?\" to see detailed instructions.\n"; - std::clog << "\nFile:\n"; + std::clog << "\nCmdFile:\n"; std::clog << "Each line of the input file(s) will be parsed as a command.\nEmpty lines and lines start with '#' will be skipped.\n"; std::clog << "\nHereDoc:\n"; @@ -450,28 +450,31 @@ void rand_data(const std::vector &cmd_units) array rd_data(tc.row_number()); rd_data.random_float(p1, p2, rd_type); - std::string col_name = "RdData"; - if (cmd_units.size() >= 5) col_name = cmd_units[4]; - - tc.add_column(col_name); - tc.fill_column(rd_data, col_name, 12); + if (cmd_units.size() >= 5) tc.fill_column(rd_data, cmd_units[4], 12); + else + { + std::string col_name = "RdData"; + tc.add_column(col_name); + tc.fill_column(rd_data, col_name, 12); + } return; } void filt_data(const std::vector &cmd_units) { - // filter row|col ,,... - if (cmd_units.size() < 4) throw std::runtime_error("filter: insufficient parameters."); + // filter row|col regex|math ,,... + if (cmd_units.size() < 5) throw std::runtime_error("filter: insufficient parameters."); std::vector tar_names; - parse_string_to_vector(cmd_units[3], ',', tar_names); + parse_string_to_vector(cmd_units[4], ',', tar_names); table_headtype_e thead; if (cmd_units[1] == "row") thead = RowHead; else if (cmd_units[1] == "col") thead = ColHead; else throw std::runtime_error("filter: invalid parameters."); - if (tar_names.size() == 1) tc.filter(cmd_units[2], tar_names[0], thead); - else tc.filter(cmd_units[2], tar_names, thead); + if (cmd_units[2] == "regex") tc.filter(cmd_units[3], tar_names[0], thead); + else if (cmd_units[2] == "math") tc.filter(cmd_units[3], tar_names, thead); + else throw std::runtime_error("filter: invalid parameters."); return; } @@ -505,7 +508,7 @@ int main(int argc, char *argv[]) display_logo(); std::clog << "dsviewer - read, manipulate and write dsv/csv files.\n"; - std::clog << "Usage: dsviewer [ ...]\n"; + std::clog << "Usage: dsviewer [ ...]\n"; std::clog << "Enter '?' to see all available commands.\n"; std::string cmd_str; diff --git a/tool/dsviewer/dsviewer.h b/tool/dsviewer/dsviewer.h index 21a687c..5528529 100644 --- a/tool/dsviewer/dsviewer.h +++ b/tool/dsviewer/dsviewer.h @@ -72,7 +72,7 @@ const cmd_pair commands[CMD_NUM] = { {"title", set_titles, "Set row and column titles."}, {"math", math_func, "Preform mathematic operations of column data."}, {"random", rand_data, "Generate random column data."}, - {"filter", filt_data, "Filter column data by value."}, + {"filter", filt_data, "Filter row and column data using regex and mathematic expressions."}, {"null", nullptr, "null"} }; diff --git a/tool/dsviewer/dsviewer.md b/tool/dsviewer/dsviewer.md index 2fafc9a..f76bc81 100644 --- a/tool/dsviewer/dsviewer.md +++ b/tool/dsviewer/dsviewer.md @@ -23,4 +23,16 @@ Show statistics of a data column or columns. If the input file has no column nam Enable table outputs. If the input file has no row or column names, use inbuild names `R` and `C` to select the rows and columns. #### disable column|row \ \ ... -Disable table outputs. If the input file has no row or column names, use inbuild names `R` and `C` to select the rows and columns. \ No newline at end of file +Disable table outputs. If the input file has no row or column names, use inbuild names `R` and `C` to select the rows and columns. + +#### title row|col \,\,\,... [\,\,\,...] +Set row and column titles. The titles will be assigned sequentially if no index is specified. + +#### math \ \,\,\... +Preform mathematic operations of column data. The user needs to write a mathematical expression using the column names as variables. Then provide the column names as the arguments, in which the resultant column's name must be placed at the beginning. For example, to calculate the sum of column `C1` and `C2`, and store the result in column `C3`, the command is: `math C3=C1+C2 C3,C1,C2`. + +#### random normal|uniform \ \ [\] +Generate random data using normal or uniform distribution. The first parameter is the mean or the lower bound, and the second parameter is the standard deviation or the upper bound. If no column name is provided, a new column will be created (titled "RdData"). + +#### filter row|col regex|math \ \,\,\... +Filter row and column data using regex and mathematic expressions. The first parameter is the expression, and the following parameters are the column names. For example, to filter rows whose column `C1` is greater than 0, the command is: `filter col math C1>0 C1`. Regex expression can be used to filter rows whose column `C1` contains the string "abc", the command is: `filter col regex abc C1`. \ No newline at end of file