[libdata-table-perl.git] 01/06: Import original source of Data-Table 1.72
Vincent Danjean
vdanjean at debian.org
Tue Feb 23 08:03:41 UTC 2016
This is an automated email from the git hooks/post-receive script.
vdanjean pushed a commit to branch master
in repository libdata-table-perl.git.
commit e32476961defe1183fc5f7ee20e722ad572b04f2
Author: Vincent Danjean <Vincent.Danjean at ens-lyon.org>
Date: Sun Feb 21 14:06:59 2016 +0100
Import original source of Data-Table 1.72
---
Category.csv | 9 +
Changes | 458 ++++++++
MANIFEST | 24 +
META.json | 44 +
META.yml | 25 +
Makefile.PL | 64 +
Product.csv | 78 ++
README | 42 +
Table.html | 1117 ++++++++++++++++++
Table.pm | 3268 +++++++++++++++++++++++++++++++++++++++++++++++++++
aaa.csv | 10 +
aaa.tsv | 10 +
bbb.csv | 12 +
ccc.csv | 3 +
colon.csv | 3 +
t/test.t | 341 ++++++
t/test1.t | 17 +
t_dos.csv | 3 +
t_dos2.csv | 3 +
t_mac.csv | 1 +
t_unix.csv | 3 +
t_unix_noheader.csv | 2 +
ttt.csv | 3 +
ttt.tsv | 3 +
24 files changed, 5543 insertions(+)
diff --git a/Category.csv b/Category.csv
new file mode 100755
index 0000000..39dc682
--- /dev/null
+++ b/Category.csv
@@ -0,0 +1,9 @@
+CategoryID,CategoryName,Description
+1,Beverages,"Soft drinks, coffees, teas, beers, and ales"
+2,Condiments,"Sweet and savory sauces, relishes, spreads, and seasonings"
+3,Confections,"Desserts, candies, and sweet breads"
+4,Dairy Products,Cheeses
+5,Grains/Cereals,"Breads, crackers, pasta, and cereal"
+6,Meat/Poultry,Prepared meats
+7,Produce,Dried fruit and bean curd
+8,Seafood,Seaweed and fish
diff --git a/Changes b/Changes
new file mode 100755
index 0000000..c1db6a7
--- /dev/null
+++ b/Changes
@@ -0,0 +1,458 @@
+Revision history for Perl extension Data::Table.
+
+1.72 Wed Apr 29 22:43:13 PDT 2015
+ No change to actual code Table.pm
+ Just comment out "Guess Windows file format" from t/test1.t, as Strawberry Perl swallows \r
+ and fails the test, however, this does not affect it to use the package
+
+1.71
+ Fixed a minor bug in rename(), where the old name was not complete deleted.
+ The bug was triggered in rowMerge(byName=>1) + a very special situations.
+ Also fixed several typos in the test code.
+
+ A special thank to Alexandr Ciornii for help prepare new Makefile.PL,
+ as well as create better test files.
+
+1.70 Sat Jan 25 06:44:12 PST 2014
+ Minor patch to 1.69, as encoding function is only reliably supported by Perl newer than v5.8.1.
+ Patch internal method openFileWithEncoding(), so that older Perl version will not give an error.
+
+ Fixed a warning in fromFileGuessOS, introduced in 1.69.
+
+1.69 Tue Jan 14 10:22:19 PST 2014
+
+ Fix a minor bug in pivot() related to colToSplitIsStringOrNumeric.
+
+ Integer column names are allowed. However, fromFile by default does not take numeric
+ column names, unless allowNumericHeader is set to 1.
+ Patch fromFile(), checkHeader(), colIndex(), fromFileIsHeader(), fromFileGetTopLines()
+ to support numeric column header.
+ An integer is first interpreted as a column name. Therefore, accessing a column by its
+ ordinal number may not be possible, if the number is used as a column name. In such case,
+ first fetch the corresponding column name and access by name.
+
+ Support file encoding methods in fromFile, fromCSV, fromTSV.
+ E.g., fromCSV("filename, 1, undef, {encoding=> 'UTF-8'})
+ UTF-8 is the default encoding, can be controlled by $Data::Table::DEFAULTS{ENCODING}.
+ Thanks to questions asked by Sergio Basto and Thomas Hofmann.
+
+ If an integer is passed to colIndex(), it is interpreted as string first for column lookup.
+ fromFile by default will allow numeric headers (but not all column headers can be numeric).
+
+ support skip_empty in melt();
+
+1.68 Mon Aug 6 22:22:22 PDT 2012
+
+ Patch fromFileGetTopLines() and fromFileIsHeader(), which are used by fromFile(). Impact: minor.
+ Improve performance of fromFileGuessOS()
+ Improve fromFile(), fromCSV(), csv() to support using \r, \n within a CSV field.
+
+ join() now supports {matchNULL => 1, NULLasEmpty => 1}, if one would like to treat NULL as empty string, or
+ treat NULL as equal (however, not equal to empty string). Both are set to 0 by default.
+ Suggested by Kyle Horton & Wilson Dave.
+
+ Remove inheritance from AutoLoader and Exporter.
+ Thanks to Brian Wightman
+
+ Thanks to Nicholas Andonakis for sharing his code, quite a few ideas in his package inspired the improvements below!
+
+ Add new shortcut methods: lastRow(), lastCol(), colName($colNumericIndex)
+ One can now write
+ foreach my $i (0..$t->lastRow)
+ instead of
+ foreach my $i (0..$t->nofRow-1)
+
+ Add iterator(), so that one can now write
+ my $next = $t_product->iterator();
+ while (my $row = $next->()) {
+ # have access to a row as a hash reference, access row number by &$next(1);
+ $t_product->setElm($next->(1), 'ProductName', 'New! '.$row->{ProductName});
+ }
+
+ addCol() can take the default value for the new column (first argument)
+ addRow() supports {addNewCol => 1}
+ moveCol() can take a $newColName.
+
+ setElm() can set a value for multiple cells, specified by ref to row array and col array
+ match_string(), match_pattern(), match_pattern_hash() also produce $parentTable->{MATCH}
+
+ # match returns all matched row ids in $t_product->{MATCH} (ref to row ID array)
+ $t_product->match_pattern_hash('$_{UnitPrice} > 20');
+ # create a new column, with 'No' as the default value
+ $t_product->addCol('No', 'IsExpensive');
+ # use $t_product->{MATCH} to set values for multiple Elements
+ $t_product->setElm($t_product->{MATCH}, 'IsExpensive', 'Yes');
+
+1.67 Wed Jul 25 11:47:23 PDT 2012
+ Update Change.txt file to point out $keepRestCol defaults to 1 is only for group()
+ For pivot(), $keepRestCol is still default to 0 as before.
+
+1.66 Wed Jul 25 11:03:29 PDT 2012
+ Change the default value of keepRestCol in group() to 1, instead of 0 to be compatible with older versions
+ Thanks to Kyle Horton
+
+1.65 Mon Jul 23 20:16:08 PDT 2012
+ Finish the "Perl Data::Table Cookbook", should be a good learning material.
+ To download, visit https://sites.google.com/site/easydatabase/
+
+ Polish Data::Table::Excel for CPAN upload.
+
+ Minor patches to the code.
+
+1.64 Sun Jul 8 22:01:17 PDT 2012
+ Add $keepRestCols to Data::Table::group();
+ We introduce new constants for fromCSV/fromTSV/fromFile/csv/tsv.
+ Data::Table::OS_UNIX = 0;
+ Data::Table::OS_PC = 1;
+ Data::Table::OS_MAC = 2;
+ Add method reorder(), redefine column orders
+ Add method melt() and cast(), concept borrowed from Reshape package in R
+ Add method each_group(), so one can apply a custom method to rows sharing the same key
+
+ Made a seemingly backward incompatible change to pivot()
+ pivot($colToSplit, $colToSplitIsNumeric, ...) is changed to
+ pivot($colToSplit, $colToSplitIsStringOrNumber, ...)
+ What is now pivot($colToSplit, $Data::Table::STRING, ...), where Data::Table::STRING has a value of 1,
+ was equivalent to pivot($colToSplit, 0, ...) in <= 1.63.
+ However, the $colToSplitIsStringOrNumber is now auto-guessed within the code, so the change is not very relevant.
+ Most existing code should run fine, without change.
+
+ Patch group(), piviot() to distinguish keys between empty string and undef.
+
+ Patch subTable() to take row mask array when {useRowMask=>1} is provided.
+
+1.63 Tue Jun 12 17:05:43 PDT 2012
+ In this release, we patch addCol, delCol, addRow, rowMerge, colMerge to for an empty table
+ We introduce new methods isEmpty(), hasCol(), moveCol($colID, $newColIdx)
+ We introduce new constants for Data::Table::new()
+ Data::Table::ROW_BASED
+ Data::Table::COL_BASED
+
+1.62 Fri May 25 11:40:09 PDT 2012
+ In this release, we address a few pain points
+
+ Data::Table::colMerge, update to support new options
+ { renameCol => 1}
+ If specified, duplicate column names in the second table is automatically renamed (by appending _2) to avoid conflict
+
+ We introduce some constants, so we have fewer numbers to remember.
+ Data::Table::NUMBER
+ Data::Table::STRING
+ Data::Table::ASC
+ Data::Table::DESC
+
+ for sort(), you can use $t->sort('col2', Data::Table::NUMBER, Data::Table::DESC); it is equivalent to $t->sort('col2', 0, 1);
+
+ Data::Table::INNER_JOIN
+ Data::Table::LEFT_JOIN
+ Data::Table::RIGHT_JOIN
+ Data::Table::FULL_JOIN
+
+ for join(), you may use $t->sort($t2, Data::Table::FULL_JOIN, ['col1'], ['col1']);
+ it is equivalent to $t->sort($t2, 3, ['col1'], ['col1']).
+
+ match_string, match_pattern have been generating @Data::Table::OK, which is a class-level array.
+ $t->match_pattern() will now also store the results (array ref) in $t->{OK}, that should be used in the future.
+ However, @Data::Table::OK is still supported for compatibility reasons.
+ This is not a pain point, but conceptually nicer to be localized.
+
+ match_pattern_hash() is added. The difference is each row is fed to the pattern as a hash %_. In the case of
+ match_pattern, each row is fed as an array ref $_. The pattern for match_pattern_hash() becomes much cleaner.
+ If a table has two columns: Col_A as the 1st column and Col_B as the 2nd column, a filter "Col_A>2 AND Col_B<2"
+ is written before as
+ $t->match_pattern('$_->[0] > 2 && $_->[1] <2');
+ where we need to figure out $t->colIndex('Col_A') is 0 and $t->colIndex('Col_B') is 1, in order to build the pattern.
+ Now you can use column name directly in the pattern:
+ $t->match_pattern_hash('$_{Col_A} >2 && $_{Col_B} <2');
+ This method creates $t->{OK}, as well as @Data::Table::OK, same as match_pattern().
+
+ Data::Table::rowMerge, update to support new options
+ { byName =>1, addNewCol => 1}
+ If byName is 1, rows in the second table are appended by matching their column names, so that the second table
+ can have columns in a different order.
+ If addNewCol is 1, columns not exist in the first table will be automatically added.
+ addNewCol is best used with byName. If used alone, addNewCol will just patch the two tables so that they have
+ the same number of columns.
+ Data::Table::subTable, update internal to remove side effect on column header array
+ Data::join add support for an option {renameCol => 1}.
+ If specified, duplicate column names in the second table is automatically renamed (by appending _2) to avoid conflict
+
+1.61 Mon Feb 27 21:07:55 PST 2012
+ Data::Table::fromSQL now can take DBI::st instead of a SQL string. This is introduced, so that
+ variable binding (such as CLOB/BLOB) can be done outside the method.
+
+1.60 Sat Feb 25 19:26:46 PST 2012
+ Data::Table::addRow now also can take a hash reference. Hash keys are column names,
+ undef will be the value, if a column name is not found in the hash.
+ Suggested by Federico
+
+1.59 Sun Feb 5 00:20:00 PST 2012
+ I have never checked those CPAN ticket, happened to discover them and address them in this version.
+ Update document, explain Data::Table::fromCSV(\*STDIN, 1) can be used to read table from STDIN.
+ Add tbody and thead to Data::Table::html, if it's portrait.
+ Suggested by Ken Rosenberry.
+ Modify Data::Table::html and Data::Table::html2, so that it can accept coloring via CSS
+ The color now can be either specified as an array as before, or as three CSS class names
+ Suggested by Xavier Robin
+
+1.58 Thu Feb 2 20:33:03 PST 2012
+ Patch join(), prior version of join considers two NULL keys to be equal
+ update document, clarify that rowMerge assumes table columns in the same order
+ Thanks to Ulrik Stervbo.
+
+1.57 Thu Apr 23 15:22:36 PDT 2009
+ Patch pivot(), it throws warning before, when colToFill is undef.
+
+1.56 Fri Aug 22 15:53:29 PDT 2008
+ When the first line in a TSV is not a header, but contains strings such as \t.
+ The program will not transform \t to a tab.
+ Modify fromTSV, so that \t, \N (etc) transformation is optional.
+ Add transform_element flag to fromTSV method to turn on/off the transformation.
+ Thanks to Bin Zhou.
+
+1.55 Mon May 5 10:29:44 PDT 2008
+ Patch parseCSV. fromFile guesses the wrong delimiter if some ending columns are empty.
+
+1.54 Sun Feb 10 21:35:02 PST 2008
+ Modify fromFileGetTopLines method, remove dependency on bytes
+ bytes::substr causes infinite loop in some older version of perl
+ Thanks to "eserte" for help in debugging.
+
+1.53 Thu Jan 3 21:13:40 PST 2008
+ add "use bytes" to Table.pm
+ Just patched test.pl, because some OS cannot open in-memory file.
+
+1.52 Fri Dec 14 11:48:42 PST 2007
+1.51 Wed Dec 12 15:36:22 PST 2007
+ 1. Add a class methods Data::Table::fromFile(file_name), which can
+ guess the file format and call fromCSV/fromTSV internally.
+
+ fromFile relies on the following new methods
+ fromFileGuessOS(file_name)
+ fromFileGetTopLines($file_name, $OS, $lineNumber)
+ fromFileIsHeader($string)
+ fromFileGuessDelimiter($arrayRefToLines)
+ to figure out if the input file is from UNIX/PC/MAC, whether its first
+ row contains column headers, and whether it uses ",", "\t" or ":" as
+ field delimiters.
+ It then calls either fromCSV or fromTSV to return the table object.
+
+ $t = Data::Table::fromFile("myFileName_CSVorTSV_HeaderOrNoHeader_UNIXorPCorMAC");
+
+ Please refers to the updated document for details.
+
+ 2. When fromFile/fromCSV/fromTSV reads from an empty file, it returns
+ an undef object, rather than quit.
+
+ 3. Provide more informative error message, when invalid column header is found.
+
+ 4. fixed a bug in 1.51 where fromFileGuessOS failed in Windows
+ Thanks to patches provided by "whitebell".
+
+1.50 Thu Sep 28 07:21:38 PDT 2006
+ Small modifications to sort subroutine example in the document, no bug.
+
+ join method, if $cols2 is undefined, defaults @$cols2 to @$cols1
+ Update fromCSV, fromTSV, csv methods to be able to deal with certain delimiters correctly.
+ (When the delimiter is a special symbol for regexp, it should be escaped, e.g., set delimiter to '\|' for pipe symbol).
+ Thanks to suggestions from Michael Slaven.
+
+ update fromCSV, fromTSV to take additional arguments: skip_lines and skip_pattern
+ skip_lines lets user skip several lines in the beginning of the input file
+ skip_pattern lets user skip all lines that match a regular expression
+ Please read documents under fromCSV and fromTSV for details.
+ Thanks to suggestions from Wenbin Ye.
+
+1.49 Wed Aug 30 09:29:51 PDT 2006
+ Add %Data::Table::DEFAULTS to store the default settings for OS, CSV_DELIMITER and CSV_QUALIFIER
+ Thanks to suggestions from Roman Filippov
+
+ Patch sort method to deal with undef table element. undef value is considered to be
+ larger than any other value, two undef values are considered equal during sorting.
+
+1.48 Thu Jun 8 13:25:54 PDT 2006
+ Update fromCSV, parseCSV to enable user-specified delimiter and qualifier,
+ see document and examples under fromCSV.
+ csvEscape is modified accordingly.
+ Thanks to help from Roman Filippov
+
+1.47 Sun May 21 15:03:14 PDT 2006
+ Upload the wrong code in 1.46, re-upload
+
+1.46 Sat May 13 05:44:09 PDT 2006
+ fromCSV, fromTSV, csv, tsv can all take either a file hander or a file name
+ Notice: to leave rooms for future development, file handler is not closed by Data::Table.
+ It's caller's responsibility to close it afterwards, if no longer used.
+ table::sort code is replaced, the old sort method is renamed to sort_v0 and is deprecated.
+ The new sort method allow user-defined sorting operators, please read manual on table::sort
+ The new sort method also runs faster in some benchmark tests.
+ A big thank to Wenbin Ye for suggestions, as well as contributing
+ both the new sort code and test examples
+
+1.45 Mon May 1 09:08:20 PDT 2006
+ Fix a bug in fromTSV
+ last column name is truncated by one character (introduced in 1.44)
+ Thanks to Albert V. Smith
+
+1.44 Sat Apr 15 04:27:28 PDT 2006
+ Fix a bug in join (type=2 and 3)
+ When right or full join, key fields are undef for right-only entries.
+
+ modify fromCSV, fromTSV, tsv, csv subroutines to support read/write PC, Mac and UNIX files.
+ csv and tsv can take a file name and directly writes to it.
+
+1.43 Tue Nov 9 10:23:44 PST 2004
+ Patch html so that valid XHTML code is generated
+ Several mispelled words were corrected
+ Thanks all to Wolfgang Dautermann
+
+1.42 Fri Oct 8 11:56:41 PDT 2004
+ Minor changes to group and pivot, not a bug
+
+1.41 Thu Oct 7 14:04:17 CDT 2004
+ Add two useful methods: group and pivot
+ group can make the records unique based on given key columns
+ pivot is handy to transfer database table into a more user readable format
+ group+pivot make accounting operations easy, please read the document for details.
+
+ Due to the spam, please use the following for email contact
+ easydatabase at gmail dot com
+
+1.40 Wed Oct 15 12:11:11 CDT 2003
+ Patch colMap, as suggested by Jeff Janes
+
+1.39 Wed Mar 26 09:33:01 CST 2003
+ Fix a bug in match_pattern, match_string and row_mask;
+ When a new table was created by these methods, deleting columns had a
+ side effect of the original parent table.
+
+1.38 Sun Jan 19 18:26:23 CST 2003
+ Change die to croak as suggested by Jeff Janes.
+
+1.37 Tue Sep 17 17:53:55 CDT 2002
+ Add $countOnly to match_string and match_pattern, thanks to Serge Batalov.
+
+1.36 Thu Sep 12 14:47:59 CDT 2002
+ Add close() to both fromTSV and fromCSV, thanks to Brian Coon.
+
+1.35 Mon Jul 1 13:04:43 PDT 2002
+ Optimization in parseCSV, thanks to Jeff Janes.
+
+1.34 Wed May 1 12:13:33 CDT 2002
+ Fix a bug in colMerge
+
+1.33 Wed Jan 16 17:55:34 CST 2002
+ Small patches to join method. Not a bug.
+ Thanks to Xiao-Jun Ma
+
+1.32 Sun Sep 30 16:21:02 CDT 2001
+ No change, just update Table.html (forgot in version 1.31)
+
+1.31 Wed Sep 20 21:22:22 PDT 2001
+ add colsMap($fun), which does more than colMap can;
+ Unlike colMap, $fun here have access to multiple columns.
+ Read document for details.
+
+1.30 Wed Sep 19 20:02:52 PDT 2001
+ Improve header method, which can now take a new header argument.
+ Improve fromTSV and fromCSV, which now can take the 3rd argument --
+ if header is supplied, it will always be used (despite the 2nd argument).
+ Read document for details.
+ Fix a bug in adding a new column to a empty table.
+ Thanks to Serge Batalov.
+
+1.29 Mon Sep 17 18:18:13 CDT 2001
+ a bug fixed in fromTSV
+ The first line was skipped when header==0 is specified in fromTSV.
+
+1.28 Wed Sep 5 17:59:36 CDT 2001
+ a bug fixed in fromCSV, where \c or \\ apears in the file.
+ Fix provided by Jeff Janes.
+
+1.27 Mon Jul 9 00:04:54 PDT 2001
+ accept more formatting parameters for html, combine html2 with html.
+
+1.26 Mon May 21 09:35:41 PDT 2001
+ A typo bug in swap fixed
+ Thanks to Jeff Janes
+
+1.25 Thu May 3 00:50:03 PDT 2001
+ add BEGIN, check perl version, update README.
+ We realize Table.pm requires 5.005 at least.
+ See README for the patch for older perl.
+ Thanks to Jeffery Cann
+
+1.24 Sat Apr 21 20:30:18 PDT 2001
+ a bug in match_pattern fixed (important!)
+ Thanks to Robson Francisco de Souza
+
+1.23 Thu Apr 12 14:36:45 PDT 2001
+ a bug in html, html2 fixed, where table element "" displayed ugly
+ introduced in version 1.21
+
+1.22 Sat Mar 25 14:30:06 PST 2001
+ join method added
+ support four join types: inner, left outer, right outer, and full outer.
+
+ a small bug in html2 fixed, thanks to Fred Lovine
+
+1.21 Fri Mar 9 21:04:30 PST 2001
+ rowMask method added
+
+ A bug in html, html2 fixed, where table element 0 is not displayed
+ Thanks to Sven Neuhaus
+
+1.20 Wed Feb 28 12:38:53 PST 2001
+ A bug in match_string is fixed.
+ This will affect results if you change your "string" value in the program;
+ Also add a caseIgnore control argument to match_string method.
+ Thanks to Bryan Coon.
+
+1.19 Sat Feb 24 23:23:37 PST 2001
+ A bug in fromSQL is fixed (caused by typo)
+ This happens when user use the third argument (a reference to an array).
+ The old package will show an error message in some cases.
+
+ Add $header option for csv and tsv, output header or not
+
+ Add the following instant methods
+ fromSQLi
+ fromCSVi
+ fromTSVi
+ so that these methods can be inherited.
+ Thanks to Michael Schlueter
+
+ Update new method, so that it can be used as an instant method as well.
+
+ Add method
+ rowHashRef
+ which returns a copy of a table row in hash reference.
+
+ Officially support TSV format via the following three methods
+ Data::Table::fromTSV
+ fromTSVi
+ tsv
+ Read "TSV FORMAT" section for details.
+
+1.18
+ Fix the problem in Data::Table::fromCSV caused by null trailing fields.
+ E.g., a line "a,b,," in a csv file was split into two fields before.
+ Thanks to Karsten
+
+ Fix the warning message in Data::Table::match_string, when table contains
+ an undef element.
+
+ Add Data::Table::fromTSV and Data::Table:tsv
+ TSV - tab-deliminated file format. TSV preserves NULL element and line-break
+ chars in a table.
+ \0, \\, \r, \b, \n, \t are slash-escaped.
+ undef is escaped into \N.
+ This is based on MySQL specification.
+
+1.16 Fri Sep 29 22:18:06 PDT 2000
+ Package name changed from Table to Data::Table, due to
+ name collision with PerlQt
+ first official release version
+
+1.15 Tue Sep 26 18:32:52 PDT 2000
+ submitted to CPAN
diff --git a/MANIFEST b/MANIFEST
new file mode 100755
index 0000000..6d86f49
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,24 @@
+Changes
+Makefile.PL
+MANIFEST
+README
+Table.pm
+t/test.t
+t/test1.t
+aaa.csv
+aaa.tsv
+bbb.csv
+ccc.csv
+t_dos.csv
+t_dos2.csv
+t_unix.csv
+t_unix_noheader.csv
+t_mac.csv
+colon.csv
+ttt.tsv
+ttt.csv
+Category.csv
+Product.csv
+Table.html
+META.yml Module YAML meta-data (added by MakeMaker)
+META.json Module JSON meta-data (added by MakeMaker)
diff --git a/META.json b/META.json
new file mode 100755
index 0000000..ba0cf79
--- /dev/null
+++ b/META.json
@@ -0,0 +1,44 @@
+{
+ "abstract" : "Data type related to database tables, spreadsheets, CSV/TSV files, HTML table displays, etc.",
+ "author" : [
+ "unknown"
+ ],
+ "dynamic_config" : 1,
+ "generated_by" : "ExtUtils::MakeMaker version 6.78, CPAN::Meta::Converter version 2.143240",
+ "license" : [
+ "perl_5"
+ ],
+ "meta-spec" : {
+ "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
+ "version" : "2"
+ },
+ "name" : "Data-Table",
+ "no_index" : {
+ "directory" : [
+ "t",
+ "inc"
+ ]
+ },
+ "prereqs" : {
+ "build" : {
+ "requires" : {
+ "ExtUtils::MakeMaker" : "0",
+ "Test::More" : "0"
+ }
+ },
+ "configure" : {
+ "requires" : {
+ "ExtUtils::MakeMaker" : "0"
+ }
+ },
+ "runtime" : {
+ "requires" : {
+ "Carp" : "0",
+ "perl" : "5.006"
+ }
+ }
+ },
+ "release_status" : "stable",
+ "resources" : {},
+ "version" : "1.72"
+}
diff --git a/META.yml b/META.yml
new file mode 100755
index 0000000..e3ebce0
--- /dev/null
+++ b/META.yml
@@ -0,0 +1,25 @@
+---
+abstract: 'Data type related to database tables, spreadsheets, CSV/TSV files, HTML table displays, etc.'
+author:
+ - unknown
+build_requires:
+ ExtUtils::MakeMaker: 0
+ Test::More: 0
+configure_requires:
+ ExtUtils::MakeMaker: 0
+dynamic_config: 1
+generated_by: 'ExtUtils::MakeMaker version 6.78, CPAN::Meta::Converter version 2.143240'
+license: perl
+meta-spec:
+ url: http://module-build.sourceforge.net/META-spec-v1.4.html
+ version: 1.4
+name: Data-Table
+no_index:
+ directory:
+ - t
+ - inc
+requires:
+ Carp: 0
+ perl: 5.006
+resources: {}
+version: 1.72
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100755
index 0000000..4e3b91b
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,64 @@
+use ExtUtils::MakeMaker;
+# See lib/ExtUtils/MakeMaker.pm for details of how to influence
+# the contents of the Makefile that is written.
+
+# perl 5.005 is required for qr() support
+# perl 5.006 is required for 2-arg binmode, autovivifying filehandles
+# see README for details
+#
+require 5.006;
+
+WriteMakefile1(
+ ABSTRACT_FROM => 'Table.pm',
+ 'NAME' => 'Data::Table',
+ 'VERSION_FROM' => 'Table.pm', # finds $VERSION
+ 'PREREQ_PM' => {
+ #bytes => 0,
+ Carp => 0,
+ },
+ MIN_PERL_VERSION => '5.006',
+ META_MERGE => {
+ resources => {
+ #repository => 'URL to repository here',
+ },
+ },
+ TEST_REQUIRES => {
+ 'Test::More' => 0,
+ },
+ 'LICENSE' => 'perl',
+ $^O =~/win/i ? (
+ dist => {
+ TAR => 'ptar',
+ TARFLAGS => '-c -C -f',
+ },
+ ) : (),
+);
+
+sub WriteMakefile1 { #Compatibility code for old versions of EU::MM. Written by Alexandr Ciornii, version 0.23. Added by eumm-upgrade.
+ my %params=@_;
+ my $eumm_version=$ExtUtils::MakeMaker::VERSION;
+ $eumm_version=eval $eumm_version;
+ die "EXTRA_META is deprecated" if exists $params{EXTRA_META};
+ die "License not specified" if not exists $params{LICENSE};
+ if ($params{AUTHOR} and ref($params{AUTHOR}) eq 'ARRAY' and $eumm_version < 6.5705) {
+ $params{META_ADD}->{author}=$params{AUTHOR};
+ $params{AUTHOR}=join(', ',@{$params{AUTHOR}});
+ }
+ if ($params{TEST_REQUIRES} and $eumm_version < 6.64) {
+ $params{BUILD_REQUIRES}={ %{$params{BUILD_REQUIRES} || {}} , %{$params{TEST_REQUIRES}} };
+ delete $params{TEST_REQUIRES};
+ }
+ if ($params{BUILD_REQUIRES} and $eumm_version < 6.5503) {
+ #EUMM 6.5502 has problems with BUILD_REQUIRES
+ $params{PREREQ_PM}={ %{$params{PREREQ_PM} || {}} , %{$params{BUILD_REQUIRES}} };
+ delete $params{BUILD_REQUIRES};
+ }
+ delete $params{CONFIGURE_REQUIRES} if $eumm_version < 6.52;
+ delete $params{MIN_PERL_VERSION} if $eumm_version < 6.48;
+ delete $params{META_MERGE} if $eumm_version < 6.46;
+ delete $params{META_ADD} if $eumm_version < 6.46;
+ delete $params{LICENSE} if $eumm_version < 6.31;
+
+ WriteMakefile(%params);
+}
+
diff --git a/Product.csv b/Product.csv
new file mode 100755
index 0000000..71f249a
--- /dev/null
+++ b/Product.csv
@@ -0,0 +1,78 @@
+ProductID,ProductName,CategoryID,UnitPrice,UnitsInStock,Discontinued
+1,Chai,1,18,39,FALSE
+2,Chang,1,19,17,FALSE
+3,Aniseed Syrup,2,10,13,FALSE
+4,Chef Anton's Cajun Seasoning,2,22,53,FALSE
+5,Chef Anton's Gumbo Mix,2,21.35,0,TRUE
+6,Grandma's Boysenberry Spread,2,25,120,FALSE
+7,Uncle Bob's Organic Dried Pears,7,30,15,FALSE
+8,Northwoods Cranberry Sauce,2,40,6,FALSE
+9,Mishi Kobe Niku,6,97,29,TRUE
+10,Ikura,8,31,31,FALSE
+11,Queso Cabrales,4,21,22,FALSE
+12,Queso Manchego La Pastora,4,38,86,FALSE
+13,Konbu,8,6,24,FALSE
+14,Tofu,7,23.25,35,FALSE
+15,Genen Shouyu,2,15.5,39,FALSE
+16,Pavlova,3,17.45,29,FALSE
+17,Alice Mutton,6,39,0,TRUE
+18,Carnarvon Tigers,8,62.5,42,FALSE
+19,Teatime Chocolate Biscuits,3,9.2,25,FALSE
+20,Sir Rodney's Marmalade,3,81,40,FALSE
+21,Sir Rodney's Scones,3,10,3,FALSE
+22,Gustaf's Knackebrod,5,21,104,FALSE
+23,Tunnbrod,5,9,61,FALSE
+24,Guarana Fantastica,1,4.5,20,TRUE
+25,NuNuCa Nub-Nougat-Creme,3,14,76,FALSE
+26,Gumbar Gummibarchen,3,31.23,15,FALSE
+27,Schoggi Schokolade,3,43.9,49,FALSE
+28,Rossle Sauerkraut,7,45.6,26,TRUE
+29,Thuringer Rostbratwurst,6,123.79,0,TRUE
+30,Nord-Ost Matjeshering,8,25.89,10,FALSE
+31,Gorgonzola Telino,4,12.5,0,FALSE
+32,Mascarpone Fabioli,4,32,9,FALSE
+33,Geitost,4,2.5,112,FALSE
+34,Sasquatch Ale,1,14,111,FALSE
+35,Steeleye Stout,1,18,20,FALSE
+36,Inlagd Sill,8,19,112,FALSE
+37,Gravad lax,8,26,11,FALSE
+38,Cote de Blaye,1,263.5,17,FALSE
+39,Chartreuse verte,1,18,69,FALSE
+40,Boston Crab Meat,8,18.4,123,FALSE
+41,Jack's New England Clam Chowder,8,9.65,85,FALSE
+42,Singaporean Hokkien Fried Mee,5,14,26,TRUE
+43,Ipoh Coffee,1,46,17,FALSE
+44,Gula Malacca,2,19.45,27,FALSE
+45,Rogede sild,8,9.5,5,FALSE
+46,Spegesild,8,12,95,FALSE
+47,Zaanse koeken,3,9.5,36,FALSE
+48,Chocolade,3,12.75,15,FALSE
+49,Maxilaku,3,20,10,FALSE
+50,Valkoinen suklaa,3,16.25,65,FALSE
+51,Manjimup Dried Apples,7,53,20,FALSE
+52,Filo Mix,5,7,38,FALSE
+53,Perth Pasties,6,32.8,0,TRUE
+54,Tourtiere,6,7.45,21,FALSE
+55,Pate chinois,6,24,115,FALSE
+56,Gnocchi di nonna Alice,5,38,21,FALSE
+57,Ravioli Angelo,5,19.5,36,FALSE
+58,Escargots de Bourgogne,8,13.25,62,FALSE
+59,Raclette Courdavault,4,55,79,FALSE
+60,Camembert Pierrot,4,34,19,FALSE
+61,Sirop d'erable,2,28.5,113,FALSE
+62,Tarte au sucre,3,49.3,17,FALSE
+63,Vegie-spread,2,43.9,24,FALSE
+64,Wimmers gute Semmelknodel,5,33.25,22,FALSE
+65,Louisiana Fiery Hot Pepper Sauce,2,21.05,76,FALSE
+66,Louisiana Hot Spiced Okra,2,17,4,FALSE
+67,Laughing Lumberjack Lager,1,14,52,FALSE
+68,Scottish Longbreads,3,12.5,6,FALSE
+69,Gudbrandsdalsost,4,36,26,FALSE
+70,Outback Lager,1,15,15,FALSE
+71,Flotemysost,4,21.5,26,FALSE
+72,Mozzarella di Giovanni,4,34.8,14,FALSE
+73,Rod Kaviar,8,15,101,FALSE
+74,Longlife Tofu,7,10,4,FALSE
+75,Rhonbrau Klosterbier,1,7.75,125,FALSE
+76,Lakkalikoori,1,18,57,FALSE
+77,Original Frankfurter grune Sobe,2,13,32,FALSE
diff --git a/README b/README
new file mode 100755
index 0000000..357e2a8
--- /dev/null
+++ b/README
@@ -0,0 +1,42 @@
+Table - Data type related to database tables, spreadsheets, CSV/TSV files, HTML table displays, etc.
+
+Copyright (c) 1998-2006 Yingyao Zhou & Guangzhou Zou
+
+See COPYRIGHT section in Table.pm for usage and distribution rights.
+
+The Table.pm file contains the documentation.
+You can obtained the most recent version from
+
+#http://www.geocities.com/easydatabase/
+# this URL is obsolete, please use CPAN URL
+
+See also http://www.perl.com/CPAN/
+
+BUILDING
+
+ perl Makefile.PL
+ make
+ make test
+ make install (if the tests look okay)
+
+PROBLEMS
+
+ 1. Perl version is older than 5.005
+ The package uses operator qr, which was introduced in 5.005
+ If you have older perl versions, follow the instructions inside Table.pm.
+ The instructions are started with '###', three steps in total --
+ comment out two lines, uncomment one line.
+
+ Instructions in Table.pm looks like the following:
+
+ ### comment out next line if your perl version < 5.005 ###
+ $r = ($caseIgn)?qr/$s/i : qr/$s/;
+
+ ### comment out the next line if your perl version < 5.005
+ if ($elm =~ /$r/) {
+ ### uncomment the next line if your perl version < 5.005
+ # if ($elm =~ /$s/ || ($elm=~ /$s/i && $caseIgn)) {
+
+ 2. Others
+ # easydatabase at yahoo.com # this email account has been flooded by spam, please use below
+ easydatabase at gmail dot com
diff --git a/Table.html b/Table.html
new file mode 100755
index 0000000..16f2010
--- /dev/null
+++ b/Table.html
@@ -0,0 +1,1117 @@
+<?xml version="1.0" ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<title>Data::Table - Data type related to database tables, spreadsheets, CSV/TSV files, HTML table displays, etc.</title>
+<meta http-equiv="content-type" content="text/html; charset=utf-8" />
+<link rev="made" href="mailto:root at gold.nonet" />
+</head>
+
+<body style="background-color: white">
+
+
+<!-- INDEX BEGIN -->
+<div name="index">
+<p><a name="__index__"></a></p>
+
+<ul>
+
+ <li><a href="#name">NAME</a></li>
+ <li><a href="#synopsis">SYNOPSIS</a></li>
+ <li><a href="#abstract">ABSTRACT</a></li>
+ <li><a href="#introduction">INTRODUCTION</a></li>
+ <li><a href="#description">DESCRIPTION</a></li>
+ <ul>
+
+ <li><a href="#field_summary">Field Summary</a></li>
+ <li><a href="#package_variables">Package Variables</a></li>
+ <li><a href="#class_methods">Class Methods</a></li>
+ <li><a href="#table_creation">Table Creation</a></li>
+ <li><a href="#table_access_and_properties">Table Access and Properties</a></li>
+ <li><a href="#table_formatting">Table Formatting</a></li>
+ <li><a href="#table_operations">Table Operations</a></li>
+ <li><a href="#table_table_manipulations">Table-Table Manipulations</a></li>
+ <li><a href="#internal_methods">Internal Methods</a></li>
+ </ul>
+
+ <li><a href="#tsv_format">TSV FORMAT</a></li>
+ <li><a href="#interface_to_other_softwares">INTERFACE TO OTHER SOFTWARES</a></li>
+ <ul>
+
+ <li><a href="#interface_to_database_and_web">Interface to Database and Web</a></li>
+ <li><a href="#interface_to_csv_tsv">Interface to CSV/TSV</a></li>
+ <li><a href="#interface_to_excel_xls_xlsx">Interface to Excel XLS/XLSX</a></li>
+ <li><a href="#interface_to_graphics_package">Interface to Graphics Package</a></li>
+ </ul>
+
+ <li><a href="#author">AUTHOR</a></li>
+ <li><a href="#see_also">SEE ALSO</a></li>
+</ul>
+
+<hr name="index" />
+</div>
+<!-- INDEX END -->
+
+<p>
+</p>
+<h1><a name="name">NAME</a></h1>
+<p>Data::Table - Data type related to database tables, spreadsheets, CSV/TSV files, HTML table displays, etc.</p>
+<p>
+</p>
+<hr />
+<h1><a name="synopsis">SYNOPSIS</a></h1>
+<pre>
+ News: The package now includes "Perl Data::Table Cookbook" (PDF), which may serve as a better learning material.
+ To download the free Cookbook, visit https://sites.google.com/site/easydatabase/</pre>
+<pre>
+ # some cool ways to use Table.pm
+ use Data::Table;</pre>
+<pre>
+ $header = ["name", "age"];
+ $data = [
+ ["John", 20],
+ ["Kate", 18],
+ ["Mike", 23]
+ ];
+ $t = Data::Table->new($data, $header, 0); # Construct a table object with
+ # $data, $header, $type=0 (consider
+ # $data as the rows of the table).
+ print $t->csv; # Print out the table as a csv file.</pre>
+<pre>
+ $t = Data::Table::fromCSV("aaa.csv"); # Read a csv file into a table object
+ ### Since version 1.51, a new method fromFile can automatically guess the correct file format
+ # either CSV or TSV file, file with or without a column header line
+ # e.g.
+ # $t = Data::Table::fromFile("aaa.csv");
+ # is equivalent.
+ print $t->html; # Display a 'portrait' HTML TABLE on web.</pre>
+<pre>
+ use DBI;
+ $dbh= DBI->connect("DBI:mysql:test", "test", "") or die $DBI::errstr;
+ my $minAge = 10;
+ $t = Data::Table::fromSQL($dbh, "select * from mytable where age >= ?", [$minAge]);
+ # Construct a table form an SQL
+ # database query.</pre>
+<pre>
+ $t->sort("age", 0, 0); # Sort by col 'age',numerical,ascending
+ print $t->html2; # Print out a 'landscape' HTML Table.</pre>
+<pre>
+ $row = $t->delRow(2); # Delete the third row (index=2).
+ $t->addRow($row, 4); # Add the deleted row back as fifth row.
+ @rows = $t->delRows([0..2]); # Delete three rows (row 0 to 2).
+ $col = $t->delCol("age"); # Delete column 'age'.
+ $t->addCol($col, "age",2); # Add column 'age' as the third column
+ @cols = $t->delCols(["name","phone","ssn"]);
+ # Delete 3 columns at the same time.</pre>
+<pre>
+ $name = $t->elm(2,"name"); # Element access
+ $t2=$t->subTable([1, 3..4],['age', 'name']);
+ # Extract a sub-table</pre>
+<pre>
+ $t->rename("Entry", "New Entry"); # Rename column 'Entry' by 'New Entry'
+ $t->replace("Entry", [1..$t->nofRow()], "New Entry");
+ # Replace column 'Entry' by an array of
+ # numbers and rename it as 'New Entry'
+ $t->swap("age","ssn"); # Swap the positions of column 'age'
+ # with column 'ssn' in the table.</pre>
+<pre>
+ $t->colMap('name', sub {return uc}); # Map a function to a column
+ $t->sort('age',0,0,'name',1,0); # Sort table first by the numerical
+ # column 'age' and then by the
+ # string column 'name' in ascending
+ # order
+ $t2=$t->match_pattern('$_->[0] =~ /^L/ && $_->[3]<0.2');
+ # Select the rows that matched the
+ # pattern specified
+ $t2=$t->match_pattern_hash('$_{"Amino acid"} =~ /^L-a/ && $_{"Grams \"(a.a.)\""}<0.2'));
+ # use column name in the pattern, method added in 1.62
+ $t2=$t->match_string('John'); # Select the rows that matches 'John'
+ # in any column</pre>
+<pre>
+ $t2=$t->clone(); # Make a copy of the table.
+ $t->rowMerge($t2); # Merge two tables
+ $t->colMerge($t2);</pre>
+<pre>
+ $t = Data::Table->new( # create an employ salary table
+ [
+ ['Tom', 'male', 'IT', 65000],
+ ['John', 'male', 'IT', 75000],
+ ['Tom', 'male', 'IT', 65000],
+ ['John', 'male', 'IT', 75000],
+ ['Peter', 'male', 'HR', 85000],
+ ['Mary', 'female', 'HR', 80000],
+ ['Nancy', 'female', 'IT', 55000],
+ ['Jack', 'male', 'IT', 88000],
+ ['Susan', 'female', 'HR', 92000]
+ ],
+ ['Name', 'Sex', 'Department', 'Salary'], 0);
+
+ sub average { # this is an subroutine calculate mathematical average, ignore NULL
+ my @data = @_;
+ my ($sum, $n) = (0, 0);
+ foreach $x (@data) {
+ next unless $x;
+ $sum += $x; $n++;
+ }
+ return ($n>0)?$sum/$n:undef;
+ }</pre>
+<pre>
+ $t2 = $t->group(["Department","Sex"],["Name", "Salary"], [sub {scalar @_}, \&average], ["Nof Employee", "Average Salary"]);
+ # For each (Department,Sex) pair, calculate the number of employees and average salary
+ $t2 = $t2->pivot("Sex", 0, "Average Salary", ["Department"]);
+ # Show average salary information in a Department by Sex spreadsheet</pre>
+<p>
+</p>
+<hr />
+<h1><a name="abstract">ABSTRACT</a></h1>
+<p>This perl package uses perl5 objects to make it easy for
+manipulating spreadsheet data among disk files, database, and Web
+publishing.</p>
+<p>A table object contains a header and a two-dimensional array of scalars.
+Four class methods Data::fromFile, Data::Table::fromCSV, Data::Table::fromTSV, and Data::Table::fromSQL allow users
+to create a table object from a CSV/TSV file or a database SQL selection in a snap.</p>
+<p>Table methods provide basic access, add, delete row(s) or column(s) operations, as well as more advanced sub-table extraction, table sorting,
+record matching via keywords or patterns, table merging, and web publishing.
+Data::Table class also provides a straightforward interface to other
+popular Perl modules such as DBI and GD::Graph.</p>
+<p>The most updated version of the Perl Data::Table Cookbook is available at
+ <a href="https://sites.google.com/site/easydatabase/">https://sites.google.com/site/easydatabase/</a></p>
+<p>We use Data::Table instead of Table, because Table.pm has already been used inside PerlQt module in CPAN.</p>
+<p>
+</p>
+<hr />
+<h1><a name="introduction">INTRODUCTION</a></h1>
+<p>A table object has three data members:</p>
+<ol>
+<li><strong><a name="_data_" class="item">$data:</a></strong>
+
+<p>a reference to an array of array-references.
+It's basically a reference to a two-dimensional array.</p>
+</li>
+<li><strong><a name="_header_" class="item">$header:</a></strong>
+
+<p>a reference to a string array. The array contains all the column names.</p>
+</li>
+<li><strong><a name="_type" class="item">$type = 1 or 0.</a></strong>
+
+<p>1 means that @$data is an array of table columns (fields) (column-based);
+0 means that @$data is an array of table rows (records) (row-based);</p>
+</li>
+</ol>
+<p>Row-based/Column-based are two internal implementations for a table object.
+E.g., if a spreadsheet consists of two columns lastname and age.
+In a row-based table, $data = [ ['Smith', 29], ['Dole', 32] ].
+In a column-based table, $data = [ ['Smith', 'Dole'], [29, 32] ].</p>
+<p>Two implementations have their pros and cons for different operations.
+Row-based implementation is better for sorting and pattern matching,
+while column-based one is better for adding/deleting/swapping columns.</p>
+<p>Users only need to specify the implementation type of the table upon its
+creation via Data::Table::new, and can forget about it afterwards.
+Implementation type of a table should be considered volatile, because
+methods switch table objects from one type into another internally.
+Be advised that row/column/element references gained via table::rowRef,
+table::rowRefs, table::colRef, table::colRefs, or table::elmRef may
+become stale after other method calls afterwards.</p>
+<p>For those who want to inherit from the Data::Table class, internal method
+table::rotate is used to switch from one implementation type into another.
+There is an additional internal assistant data structure called
+colHash in our current implementation. This hash
+table stores all column names and their corresponding column index number as
+key-value pairs for fast conversion. This gives users an option to use
+column name wherever a column ID is expected, so that user don't have to use
+table::colIndex all the time. E.g., you may say
+$t->rename('oldColName', 'newColName')
+instead of $t->rename($t->colIndex('oldColName'), 'newColIdx').</p>
+<p>
+</p>
+<hr />
+<h1><a name="description">DESCRIPTION</a></h1>
+<p>
+</p>
+<h2><a name="field_summary">Field Summary</a></h2>
+<dl>
+<dt><strong><a name="data_refto_arrayof_refto_array" class="item">data refto_arrayof_refto_array</a></strong></dt>
+
+<dd>
+<p>contains a two-dimensional spreadsheet data.</p>
+</dd>
+<dt><strong><a name="header_refto_array" class="item">header refto_array</a></strong></dt>
+
+<dd>
+<p>contains all column names.</p>
+</dd>
+<dt><strong><a name="type_0_1" class="item">type 0/1</a></strong></dt>
+
+<dd>
+<p>0 is row-based, 1 is column-based, describe the orientation of @$data.</p>
+</dd>
+</dl>
+<p>
+</p>
+<h2><a name="package_variables">Package Variables</a></h2>
+<dl>
+<dt><strong><a name="_data__table__version" class="item">$Data::Table::VERSION</a></strong></dt>
+
+<dt><strong><a name="_data__table__ok" class="item">@Data::Table::OK</a></strong></dt>
+
+<dd>
+<p>see table::match_string, table::match_pattern, and table::match_pattern_hash
+Since 1.62, we recommend you to use $table->{OK} instead, which is a local array reference.</p>
+</dd>
+<dt><strong><a name="_data__table__match" class="item">@Data::Table::MATCH</a></strong></dt>
+
+<dd>
+<p>see table::match_string, table::match_pattern, and table::match_pattern_hash
+Since 1.67, we return the matched row indices in an array. Data::Table::MATCH is this array reference.
+Here is an example of setting a max price of 20 to all items with UnitPrice > 20.</p>
+<pre>
+ $t_product->match_pattern_hash('$_{UnitPrice} > 20');
+ $t_product->setElm($t_product->{MATCH}, 'UnitPrice', 20);</pre>
+</dd>
+<dt><strong><a name="_data__table__defaults" class="item">%Data::Table::DEFAULTS</a></strong></dt>
+
+<dd>
+<p>Store default settings, currently it contains CSV_DELIMITER (set to ','), CSV_QUALIFER (set to '"'), and OS (set to 0).
+see table::fromCSV, table::csv, table::fromTSV, table::tsv for details.</p>
+</dd>
+</dl>
+<p>
+</p>
+<h2><a name="class_methods">Class Methods</a></h2>
+<p>Syntax: return_type method_name ( [ parameter [ = default_value ]] [, parameter [ = default_value ]] )</p>
+<p>If method_name starts with table::, this is an instance method, it can be used as $t->method( parameters ), where $t is a table reference.</p>
+<p>If method_name starts with Data::Table::, this is a class method, it should be called as
+ Data::Table::method, e.g., $t = Data::Table::fromCSV("filename.csv").</p>
+<p>Conventions for local variables:</p>
+<pre>
+ colID: either a numerical column index or a column name;
+ rowIdx: numerical row index;
+ rowIDsRef: reference to an array of column IDs;
+ rowIdcsRef: reference to an array of row indices;
+ rowRef, colRef: reference to an array of scalars;
+ data: ref_to_array_of_ref_to_array of data values;
+ header: ref to array of column headers;
+ table: a table object, a blessed reference.</pre>
+<p>
+</p>
+<h2><a name="table_creation">Table Creation</a></h2>
+<dl>
+<dt><strong><a name="new" class="item">table Data::Table::new ( $data = [], $header = [], $type = 0, $enforceCheck = 1)</a></strong></dt>
+
+<dd>
+<p>create a new table.
+It returns a table object upon success, undef otherwise.
+$data: points to the spreadsheet data.
+$header: points to an array of column names. Before version 1.69, a column name must have at least one non-digit character. Since verison 1.69, this is relaxed. Although integer and numeric column names can now be accepted, when accessing a column by integer, it is first interpreted as a column name.
+$type: 0 or 1 for row-based/column-based spreadsheet.
+$enforceCheck: 1/0 to turn on/off initial checking on the size of each row/column to make sure the data arguement indeed points to a valid structure.
+In 1.63, we introduce constants Data::Table::ROW_BASED and Data::Table::COL_BASED as synonyms for $type. To create an empty Data::Table, use new Data::Table([], [], Data::Table::ROW_BASED);</p>
+</dd>
+<dt><strong><a name="subtable" class="item">table table::subTable ($rowIdcsRef, $colIDsRef, $arg_ref)</a></strong></dt>
+
+<dd>
+<p>create a new table, which is a subset of the original.
+It returns a table object.
+$rowIdcsRef: points to an array of row indices (or a true/false row mask array).
+$colIDsRef: points to an array of column IDs.
+The function make a copy of selected elements from the original table.
+Undefined $rowIdcsRef or $colIDsRef is interpreted as all rows or all columns.
+The elements in $colIDsRef may be modified as a side effect before version 1.62, fixed in 1.62.
+If $arg_ref->{useRowMask} is set to 1, $rowIdcsRef is a true/false row mask array, where rows marked as TRUE will be returned. Row mask array is typically the Data::Table::OK set by match_string/match_pattern/match_pattern_hash methods.</p>
+</dd>
+<dt><strong><a name="table_table_clone" class="item">table table::clone</a></strong></dt>
+
+<dd>
+<p>make a clone of the original.
+It return a table object, equivalent to table::subTable(undef,undef).</p>
+</dd>
+<dt><strong><a name="fromcsv" class="item">table Data::Table::fromCSV ($name_or_handler, $includeHeader = 1, $header = ["col1", ... ], {OS=>$Data::Table::DEFAULTS{'OS'}, delimiter=>$Data::Table::DEFAULTS{'CSV_DELIMITER'}, qualifier=>$Data::Table::DEFAULTS{'CSV_QUALIFIER'}, skip_lines=>0, skip_pattern=>undef, encoding=>$Data::Table::DEFAULTS{'ENCODING'}})</a></strong></dt>
+
+<dd>
+<p>create a table from a CSV file.
+return a table object.
+$name_or_handler: the CSV file name or an already opened file handler. If a handler is used, it's not closed upon return. To read from STDIN, use Data::Table::fromCSV(\*STDIN, 1).
+$includeHeader: 0 or 1 to ignore/interpret the first line in the file as column names,
+If it is set to 0, the array in $header is used. If $header is not supplied, the default column names are "col1", "col2", ...
+optional named argument OS specifies under which operating system the CSV file was generated. 0 for UNIX, 1 for PC and 2 for MAC. If not specified, $Data::Table::DEFAULTS{'OS'} is used, which defaults to UNIX. Basically linebreak is defined as "\n", "\r\n" and "\r" for three systems, respectively.</p>
+<p>optional name argument delimiter and qualifier let user replace comma and double-quote by other meaningful single characters. <b>Exception</b>: if the delimiter or the qualifier is a special symbol in regular expression, you must escape it by '\'. For example, in order to use pipe symbol as the delimiter, you must specify the delimiter as '\|'.</p>
+<p>optional name argument skip_lines let you specify how many lines in the csv file should be skipped, before the data are interpretted.</p>
+<p>optional name argument skip_pattern let you specify a regular expression. Lines that match the regular expression will be skipped.</p>
+<p>optional name argument encoding let you specify an encoding method of the csv file. This option is added to fromCSV, fromTSV, fromFile since version 1.69.</p>
+<p>The following example reads a DOS format CSV file and writes a MAC format:</p>
+<pre>
+ $t = Data::Table:fromCSV('A_DOS_CSV_FILE.csv', 1, undef, {OS=>1});
+ $t->csv(1, {OS=>2, file=>'A_MAC_CSV_FILE.csv'});
+ open(SRC, 'A_DOS_CSV_FILE.csv') or die "Cannot open A_DOS_CSV_FILE.csv to read!";
+ $t = Data::Table::fromCSV(\*SRC, 1);
+ close(SRC);</pre>
+<p>The following example reads a non-standard CSV file with : as the delimiter, ' as the qaulifier</p>
+<pre>
+ my $s="col_A:col_B:col_C\n1:2, 3 or 5:3.5\none:'one:two':'double\", single'''";
+ open my $fh, "<", \$s or die "Cannot open in-memory file\n";
+ my $t_fh=Data::Table::fromCSV($fh, 1, undef, {delimiter=>':', qualifier=>"'"});
+ close($fh);
+ print $t_fh->csv;
+ # convert to the standard CSV (comma as the delimiter, double quote as the qualifier)
+ # col_A,col_B,col_C
+ # 1,"2, 3 or 5",3.5
+ # one,one:two,"double"", single'"
+ print $t->csv(1, {delimiter=>':', qualifier=>"'"}); # prints the csv file use the original definition</pre>
+<p>The following example reads bbb.csv file (included in the package) by skipping the first line (skip_lines=>1), then treats any line that starts with '#' (or space comma) as comments (skip_pattern=>'^\s*#'), use ':' as the delimiter.</p>
+<pre>
+ $t = Data::Table::fromCSV("bbb.csv", 1, undef, {skip_lines=>1, delimiter=>':', skip_pattern=>'^\s*#'});</pre>
+<p>Use the optional name argument encoding to specify file encoding method.
+ $t = Data::Table::fromCSV("bbb.csv", 1, undef, {encoding=>'UTF-8'});</p>
+</dd>
+<dt><strong><a name="fromcsvi" class="item">table table::fromCSVi ($name, $includeHeader = 1, $header = ["col1", ... ])</a></strong></dt>
+
+<dd>
+<p>Same as Data::Table::fromCSV. However, this is an instant method (that's what 'i' stands for), which can be inherited.</p>
+</dd>
+<dt><strong><a name="fromtsv" class="item">table Data::Table::fromTSV ($name, $includeHeader = 1, $header = ["col1", ... ], {OS=>$Data::Table::DEFAULTS{'OS'}, skip_lines=>0, skip_pattern=>undef, transform_element=>1, encoding=>$Data::Table::DEFAULTS{'ENCODING'}})</a></strong></dt>
+
+<dd>
+<p>create a table from a TSV file.
+return a table object.
+$name: the TSV file name or an already opened file handler. If a handler is used, it's not closed upon return. To read from STDIN, use Data::Table::fromTSV(\*STDIN, 1).
+$includeHeader: 0 or 1 to ignore/interpret the first line in the file as column names,
+If it is set to 0, the array in $header is used. If $header is not supplied, the default column names are "col1", "col2", ...
+optional named argument OS specifies under which operating system the TSV file was generated. 0 for UNIX, 1 for P
+C and 2 for MAC. If not specified, $Data::Table::DEFAULTS{'OS'} is used, which defaults to UNIX. Basically linebreak is defined as "\n", "\r\n" and "\r" for three systems, respectively. <b>Exception</b>: if the delimiter or the qualifier is a special symbol in regular expression, you must escape it by '\'. For example, in order to use pipe symbol as the delimiter, you must specify the delimiter as '\|'.</p>
+<p>optional name argument skip_lines let you specify how many lines in the csv file should be skipped, before the data are interpretted.</p>
+<p>optional name argument skip_pattern let you specify a regular expression. Lines that match the regular expression will be skipped.</p>
+<p>optional name argument transform_element let you switch on/off \t to tab, \N to undef (etc.) transformation. See TSV FORMAT for details. However, elements are always transformed when export table to tsv format, because not escaping an element containing a tab will be disasterous.</p>
+<p>optional name arugment encoding enables one to provide an encoding method when open the tsv file.</p>
+<p>See similar examples under Data::Table::fromCSV;</p>
+<p>Note: read "TSV FORMAT" section for details.</p>
+</dd>
+<dt><strong><a name="fromtsvi" class="item">table table::fromTSVi ($name, $includeHeader = 1, $header = ["col1", ... ])</a></strong></dt>
+
+<dd>
+<p>Same as Data::Table::fromTSV. However, this is an instant method (that's what 'i' stands for), which can be inherited.</p>
+</dd>
+<dt><strong><a name="fromfile" class="item">table Data::Table::fromFile ($file_name, $arg_ref = {linesChecked=>2, allowNumericHeader=>0, encoding=>$Data::Table::DEFAULTS{'ENCODING'}})</a></strong></dt>
+
+<dd>
+<p>create a table from a text file.
+return a table object.
+$file_name: the file name (cannot take a file handler).
+linesChecked: the first number of lines used for guessing the input format. The delimiter will have to produce the same number of columns for these lines. By default only check the first 2 lines, 0 means all lines in the file.
+$arg_ref can take additional parameters, such as OS, has_header, delimiter, transform_element, etc. Encoding allows one to specify encoding methods used to open the file, which defaults to UTF-8.</p>
+<p>fromFile is added after version 1.51. It relies on the following new methods to automatically figure out the correct file format in order to call fromCSV or fromTSV internally:</p>
+<pre>
+ fromFileGuessOS($file_name, {encoding=>'UTF-8'})
+ returns integer, 0 for UNIX, 1 for PC, 2 for MAC
+ fromFileGetTopLines($file_name, $os, $lineNumber, {encoding=>'UTF-8'}) # $os defaults to fromFileGuessOS($file_name), if not specified
+ returns an array of strings, each string represents each row with linebreak removed.
+ fromFileGuessDelimiter($lineArrayRef) # guess delimiter from ",", "\t", ":";
+ returns the guessed delimiter string.
+ fromFileIsHeader($line_concent, $delimiter, $allowNumericHeader) # $delimiter defaults to $Data::Table::DEFAULTS{'CSV_DELIMITER'}
+ returns 1 or 0.</pre>
+<p>It first ask fromFileGuessOS to figure out which OS (UNIX, PC or MAC) generated the input file. The fetch the first linesChecked lines using fromFileGetTopLines. It then guesses the best delimiter using fromFileGuessDelimiter, then it checks if the first line looks like a column header row using fromFileIsHeader. Since fromFileGuessOS and fromFileGetTopLines needs to open/close the input file, these methods can only take file name, not file handler. If user specify formatting paramete [...]
+<p>fromFileGuessOS finds the linebreak that gives shortest first line (in the priority of UNIX, PC, MAC upon tie).
+fromFileGuessDelimiter works based on the assumption that the correct delimiter will produce equal number of columns for the given rows. If multiple matches, it chooses the delimiter that gives maximum number of columns. If none matches, it returns the default delimiter.
+fromFileIsHeader works based on the assumption that no column header can be empty or numeric values. However, if we allow numeric column names (especially integer column names), set {allowNumericHeader => 1}</p>
+</dd>
+<dt><strong><a name="fromsql" class="item">table Data::Table::fromSQL ($dbh, $sql, $vars)</a></strong></dt>
+
+<dd>
+<p>create a table from the result of an SQL selection query.
+It returns a table object upon success or undef otherwise.
+$dbh: a valid database handler.
+Typically $dbh is obtained from DBI->connect, see "Interface to Database" or DBI.pm.
+$sql: an SQL query string or a DBI::st object (starting in version 1.61).
+$vars: optional reference to an array of variable values,
+required if $sql contains '?'s which need to be replaced
+by the corresponding variable values upon execution, see DBI.pm for details.
+Hint: in MySQL, Data::Table::fromSQL($dbh, 'show tables from test') will also create a valid table object.</p>
+<p>Data::Table::fromSQL now can take DBI::st instead of a SQL string. This is introduced, so that variable binding (such as CLOB/BLOB) can be done outside the method, for example:</p>
+<pre>
+ $sql = 'insert into test_table (id, blob_data) values (1, :val)';
+ $sth = $dbh->prepare($sql);
+ $sth->bind_param(':val', $blob, {ora_type => SQLT_BIN});
+ Data::Table::fromSQL($dbh, $sth);</pre>
+</dd>
+<dt><strong><a name="fromsqli" class="item">table Data::Table::fromSQLi ($dbh, $sql, $vars)</a></strong></dt>
+
+<dd>
+<p>Same as Data::Table::fromSQL. However, this is an instant method (that's what 'i' stands for), whic
+h can be inherited.</p>
+</dd>
+</dl>
+<p>
+</p>
+<h2><a name="table_access_and_properties">Table Access and Properties</a></h2>
+<dl>
+<dt><strong><a name="colindex" class="item">int table::colIndex ($colID)</a></strong></dt>
+
+<dd>
+<p>translate a column name into its numerical position, the first column has index 0 as in as any perl array.
+return -1 for invalid column names.</p>
+<p>Since 1.69, we allow integer to be used as a column header. The integer $colID will first be checked against column names, if matched, the corresponding column index is returned. E.g., if column name for the 3rd column is "1", <a href="#colindex"><code>colIndex(1)</code></a> will return 2 instead of 1! In such case, if one need to access the second column, one has to access it by column name, i.e., $t->col(($t->header)[1]).</p>
+</dd>
+<dt><strong><a name="int_table_nofcol" class="item">int table::nofCol</a></strong></dt>
+
+<dd>
+<p>return number of columns.</p>
+</dd>
+<dt><strong><a name="int_table_nofrow" class="item">int table::nofRow</a></strong></dt>
+
+<dd>
+<p>return number of rows.</p>
+</dd>
+<dt><strong><a name="int_table_lastcol" class="item">int table::lastCol</a></strong></dt>
+
+<dd>
+<p>return the index of the last columns, i.e., nofCol - 1.</p>
+</dd>
+<dt><strong><a name="int_table_lastrow" class="item">int table::lastRow</a></strong></dt>
+
+<dd>
+<p>return the index of the last rows, i.e., nofRow - 1; This is syntax sugar.</p>
+<pre>
+ # these two are equivalent
+ foreach my $i (0 .. $t->lastRow)
+ foreach my $i (0 .. $t->nofRow - 1)</pre>
+</dd>
+<dt><strong><a name="bool_table_isempty" class="item">bool table::isEmpty</a></strong></dt>
+
+<dd>
+<p>return whether the table has any column, introduced in 1.63.</p>
+</dd>
+<dt><strong><a name="hascol" class="item">bool table::hasCol($colID)</a></strong></dt>
+
+<dd>
+<p>returns whether the colID is a table column, introduced in 1.63.</p>
+</dd>
+<dt><strong><a name="colname" class="item">bool table::colName($colNumericIndex)</a></strong></dt>
+
+<dd>
+<p>returns the column name for a numeric column index, notice the first column has an index of 0. Introduced in 1.68.</p>
+</dd>
+<dt><strong><a name="elm" class="item">scalar table::elm ($rowIdx, $colID)</a></strong></dt>
+
+<dd>
+<p>return the value of a table element at [$rowIdx, $colID],
+undef if $rowIdx or $colID is invalid.</p>
+</dd>
+<dt><strong><a name="elmref" class="item">refto_scalar table::elmRef ($rowIdx, $colID)</a></strong></dt>
+
+<dd>
+<p>return the reference to a table element at [$rowIdx, $colID], to allow possible modification.
+It returns undef for invalid $rowIdx or $colID.</p>
+</dd>
+<dt><strong><a name="header" class="item">array table::header ($header)</a></strong></dt>
+
+<dd>
+<p>Without argument, it returns an array of column names.
+Otherwise, use the new header.</p>
+</dd>
+<dt><strong><a name="int_table_type" class="item">int table::type</a></strong></dt>
+
+<dd>
+<p>return the implementation type of the table (row-based/column-based) at the time,
+be aware that the type of a table should be considered as volatile during method calls.</p>
+</dd>
+</dl>
+<p>
+</p>
+<h2><a name="table_formatting">Table Formatting</a></h2>
+<dl>
+<dt><strong><a name="csv" class="item">string table::csv ($header, {OS=>$Data::Table::DEFAULTS{'OS'}, file=>undef, delimiter=>$Data::Table::DEFAULTS{'CSV_DELIMITER'}, qualifier=>$Data::Table::DEFAULTS{'CSV_QAULIFIER'}})</a></strong></dt>
+
+<dd>
+<p>return a string corresponding to the CSV representation of the table.
+$header controls whether to print the header line, 1 for yes, 0 for no.
+optional named argument OS specifies for which operating system the CSV file is generated. 0 for UNIX, 1 for P
+C and 2 for MAC. If not specified, $Data::Table::DEFAULTS{'OS'} is used. Basically linebreak is defined as "\n", "\r\n" and "\r" for three systems, respectively.
+if 'file' is given, the csv content will be written into it, besides returning the string.
+One may specify custom delimiter and qualifier if the other than default are desired.</p>
+</dd>
+<dt><strong><a name="string_table_tsv" class="item">string table::tsv</a></strong></dt>
+
+<dd>
+<p>return a string corresponding to the TSV representation of the table.
+$header controls whether to print the header line, 1 for yes, 0 for no.
+optional named argument OS specifies for which operating system the TSV file is generated. 0 for UNIX, 1 for P
+C and 2 for MAC. If not specified, $Data::Table::DEFAULTS{'OS'} is used. Basically linebreak is defined as "\n", "\r\n" and "\r" for three systems, respectively.
+if 'file' is given, the tsv content will be written into it, besides returning the string.</p>
+<p>Note: read "TSV FORMAT" section for details.</p>
+</dd>
+<dt><strong><a name="html" class="item">string table::html ($colorArrayRef_or_colorHashRef = ["#D4D4BF","#ECECE4","#CCCC99"],
+ $tag_tbl = {border => '1'},
+ $tag_tr = {align => 'left'},
+ $tag_th = {align => 'center'},
+ $tag_td = {col3 => 'align="right" valign="bottom"', 4 => 'align="left"'},
+ $l_portrait = 1
+ )</a></strong></dt>
+
+<dd>
+<p>return a string corresponding to a 'Portrait/Landscape'-style html-tagged table.
+$colorArrayRef_or_colorHashRef: If a hash reference is provided, it will take three CSS class names for odd data rows, even data rows and for the header row. The default hash is {even=>"data_table_even", odd=>"data_table_odd", header=>"data_table_header").
+If a hash reference is not found, a reference to an array of three color strings is expected to provided for backgrounds for even-row records, odd-row records, and -der row, respectively. A default color array ("#D4D4BF","#ECECE4","#CCCC99") will be used if $colors isn't defined.</p>
+<p>Before version 1.59, the parameter can only accept an array reference.</p>
+<p>$tag_tbl: a reference to a hash that specifies any legal attributes such as name, border,
+id, class, etc. for the TABLE tag.</p>
+<p>$tag_tr: a reference to a hash that specifies any legal attributes for the TR tag.</p>
+<p>$tag_th: a reference to a hash that specifies any legal attributes for the TH tag.</p>
+<p>$tag_td: a reference to a hash that specifies any legal attributes for the TD tag.</p>
+<p>Notice $tag_tr and $tag_th controls all the rows and columns of the whole table. The keys of the hash are the attribute names in these cases. However, $tag_td is column specific, i.e., you should specify TD attributes for every column separately.
+The key of %$tag_td are either column names or column indices, the value is the full string to be inserted into the TD tag. E.g., $tag_td = {col3 => 'align=right valign=bottom} only change the TD tag in "col3" to be <TD align=right valign=bottom>.</p>
+<p>$portrait controls the layout of the table. The default is 1, i.e., the table is shown in the
+"Portrait" style, like in Excel. 0 means "Landscape". Since version 1.59, tbody and thead tags are added to the portrait mode output.</p>
+<p>Attention: You will have to escape HTML-Entities yourself (for example '<' as '<'), if you have characters in you table which need to be escaped. You can do this for example with the escapeHTML-function from CGI.pm (or the HTML::Entities module).</p>
+<pre>
+ use CGI qw(escapeHTML);
+ [...]
+ $t->colMap($columnname, sub{escapeHTML($_)}); # for every column, where HTML-Entities occur.</pre>
+</dd>
+<dt><strong><a name="html2" class="item">string table::html2 ($colors = ["#D4D4BF","#ECECE4","#CCCC99"],
+ $specs = {'name' => '', 'border' => '1', ...})</a></strong></dt>
+
+<dd>
+<p>This method is deprecated. It's here for compatibility. It now simple call html method with $portrait = 0, see previous description.</p>
+<p>return a string corresponding to a "Landscape" html-tagged table.
+This is useful to present a table with many columns, but very few entries.
+Check the above table::html for parameter descriptions.</p>
+</dd>
+</dl>
+<p>
+</p>
+<h2><a name="table_operations">Table Operations</a></h2>
+<dl>
+<dt><strong><a name="setelm" class="item">int table::setElm ($rowIdx, $colID, $val)</a></strong></dt>
+
+<dd>
+<p>modify the value of a table element at [$rowIdx, $colID] to a new value $val.
+It returns 1 upon success, undef otherwise.
+In 1.68, setElm can manipulate multiple elements, i.e., $rowIdx and $colIdx can be references to an index array, and <a href="#setelm"><code>setElm()</code></a> will modifies all cells defined by the grid.</p>
+<pre>
+ $t->setElm([0..2], ['ColA', 'ColB'], 'new value');
+ $t->setElm(0, [1..2], 'new value');</pre>
+<pre>
+ # puts a limit on the price of all expensive items
+ $t_product->match_pattern_hash('$_{UnitPrice} > 20');
+ $t_product->setElm($t_product->{MATCH}, 'UnitPrice', 20);</pre>
+</dd>
+<dt><strong><a name="addrow" class="item">int table::addRow ($rowRef, $rowIdx = table::nofRow, $arg_ref = {addNewCol => 0})</a></strong></dt>
+
+<dd>
+<p>add a new row ($rowRef may point to the actual list of scalars, or it can be a hash_ref (supported since version 1.60)). If $rowRef points to a hash, the method will lookup the value of a field by ts column name: $rowRef->{colName}, if not found, undef is used for that field.
+The new row will be referred as $rowIdx as the result. E.g., addRow($aRow, 0) will put the new row as the very first row. By default, it appends a row to the end.
+In 1.67, we support {addNewCol => 1}, if specified, a new column will be automatically created for each new element encountered in the $rowRef.</p>
+<pre>
+ # automatically add a new column "aNewColumn" to $t, in order to hold the new value
+ $t->addRow({anExistingColumn => 123, aNewColumn => "XYZ"}, undef, {addNewCol => 1});
+ # $t only had one column, after this call, it will contain a new column 'col2', in order to hold the new value
+ $t->addRow([123, "XYZ"], undef, {addNewCol => 1});</pre>
+<p>It returns 1 upon success, undef otherwise.</p>
+</dd>
+<dt><strong><a name="delrow" class="item">refto_array table::delRow ( $rowIdx )</a></strong></dt>
+
+<dd>
+<p>delete a row at $rowIdx. It will the reference to the deleted row.</p>
+</dd>
+<dt><strong><a name="delrows" class="item">refto_array table::delRows ( $rowIdcsRef )</a></strong></dt>
+
+<dd>
+<p>delete rows in @$rowIdcsRef. It will return an array of deleted rows in the same order of $rowIdcsRef upon success.
+upon success.</p>
+</dd>
+<dt><strong><a name="addcol" class="item">int table::addCol ($colRef, $colName, $colIdx = numCol)</a></strong></dt>
+
+<dd>
+<p>add a new column ($colRef points to the actual data), the new column will be referred as $colName or $colIdx as the result. E.g., addCol($aCol, 'newCol', 0) will put the new column as the very first column.
+By default, append a column to the end.
+It will return 1 upon success or undef otherwise.
+In 1.68, $colRef can be a scalar, which is the default value that can be used to create the new column. E.g., to create a new column with default value of undef, 0, 'default', respectively, one can do:</p>
+<pre>
+ $t->addCol(undef, 'NewCol');
+ $t->addCol(0, 'NewIntCol');
+ $t->addCol('default', 'NewStringCol');</pre>
+</dd>
+<dt><strong><a name="delcol" class="item">refto_array table::delCol ($colID)</a></strong></dt>
+
+<dd>
+<p>delete a column at $colID
+return the reference to the deleted column.</p>
+</dd>
+<dt><strong><a name="delcols" class="item">arrayof_refto_array table::delCols ($colIDsRef)</a></strong></dt>
+
+<dd>
+<p>delete a list of columns, pointed by $colIDsRef. It will
+return an array of deleted columns in the same order of $colIDsRef upon success.</p>
+</dd>
+<dt><strong><a name="rowref" class="item">refto_array table::rowRef ($rowIdx)</a></strong></dt>
+
+<dd>
+<p>return a reference to the row at $rowIdx
+upon success or undef otherwise.</p>
+</dd>
+<dt><strong><a name="rowrefs" class="item">refto_arrayof_refto_array table::rowRefs ($rowIdcsRef)</a></strong></dt>
+
+<dd>
+<p>return a reference to array of row references upon success, undef otherwise.</p>
+</dd>
+<dt><strong><a name="row" class="item">array table::row ($rowIdx)</a></strong></dt>
+
+<dd>
+<p>return a copy of the row at $rowIdx
+upon success or undef otherwise.</p>
+</dd>
+<dt><strong><a name="rowhashref" class="item">refto_hash table::rowHashRef ($rowIdx)</a></strong></dt>
+
+<dd>
+<p>return a reference to a hash, which contains a copy of the row at $rowIdx,
+upon success or undef otherwise. The keys in the hash are column names, and
+the values are corresponding elements in that row. The hash is a copy, therefore modifying the hash values doesn't change the original table.</p>
+</dd>
+<dt><strong><a name="colref" class="item">refto_array table::colRef ($colID)</a></strong></dt>
+
+<dd>
+<p>return a reference to the column at $colID
+upon success.</p>
+</dd>
+<dt><strong><a name="colrefs" class="item">refto_arrayof_refto_array table::colRefs ($colIDsRef)</a></strong></dt>
+
+<dd>
+<p>return a reference to array of column references upon success.</p>
+</dd>
+<dt><strong><a name="col" class="item">array table::col ($colID)</a></strong></dt>
+
+<dd>
+<p>return a copy to the column at $colID
+upon success or undef otherwise.</p>
+</dd>
+<dt><strong><a name="rename" class="item">int table::rename ($colID, $newName)</a></strong></dt>
+
+<dd>
+<p>rename the column at $colID to a $newName
+(the newName must be valid,
+and should not be identical to any other existing column names).
+It returns 1 upon success
+or undef otherwise.</p>
+</dd>
+<dt><strong><a name="replace" class="item">refto_array table::replace ($oldColID, $newColRef, $newName)</a></strong></dt>
+
+<dd>
+<p>replace the column at $oldColID by the array pointed by $newColRef, and renamed it to $newName. $newName is optional if you don't want to rename the column.
+It returns 1 upon success or undef otherwise.</p>
+</dd>
+<dt><strong><a name="swap" class="item">int table::swap ($colID1, $colID2)</a></strong></dt>
+
+<dd>
+<p>swap two columns referred by $colID1 and $colID2.
+It returns 1 upon success or undef otherwise.</p>
+</dd>
+<dt><strong><a name="movecol" class="item">int table::moveCol($colID, $colIdx, $newColName)</a></strong></dt>
+
+<dd>
+<p>move column referred by $colID to a new location $colIdx.
+If $newColName is specified, the column will be renamed as well.
+It returns 1 upon success or undef otherwise.</p>
+</dd>
+<dt><strong><a name="reorder" class="item">int table::reorder($colIDRefs, $arg_ref)</a></strong></dt>
+
+<dd>
+<p>Rearrange the columns according to the order specified in $colIDRef. Columns not specified in the reference array will be appended to the end!
+If one would like to drop columns not specified, set $arg_ref to {keepRest => 0}.
+<a href="#reorder"><code>reorder()</code></a> changes the table itself, while subTable(undef, $colIDRefs) will return a new table. <a href="#reorder"><code>reorder()</code></a> might also runs faster than subTable, as elements may not need to be copied.</p>
+</dd>
+<dt><strong><a name="colmap" class="item">int table::colMap ($colID, $fun)</a></strong></dt>
+
+<dd>
+<p>foreach element in column $colID, map a function $fun to it.
+It returns 1 upon success or undef otherwise.
+This is a handy way to format a column. E.g. if a column named URL contains URL strings, colMap("URL", sub {"<a href='$_'>$_</a>"}) before <a href="#html"><code>html()</code></a> will change each URL into a clickable hyper link while displayed in a web browser.</p>
+</dd>
+<dt><strong><a name="colsmap" class="item">int table::colsMap ($fun)</a></strong></dt>
+
+<dd>
+<p>foreach row in the table, map a function $fun to it.
+It can do whatever colMap can do and more.
+It returns 1 upon success or undef otherwise.
+colMap function only give $fun access to the particular element per row, while colsMap give $fun full access to all elements per row. E.g. if two columns named duration and unit (["2", "hrs"], ["30", "sec"]). colsMap(sub {$_->[0] .= " (".$_->[1].")"; } will change each row into (["2 hrs", "hrs"], ["30 sec", "sec"]).
+As show, in the $func, a column element should be referred as $_->[$colIndex].</p>
+</dd>
+<dt><strong><a name="sort" class="item">int table::sort($colID1, $type1, $order1, $colID2, $type2, $order2, ... )</a></strong></dt>
+
+<dd>
+<p>sort a table in place.
+First sort by column $colID1 in $order1 as $type1, then sort by $colID2 in $order2 as $type2, ...
+$type is 0 for numerical and 1 for others;
+$order is 0 for ascending and 1 for descending;</p>
+<p>In 1.62, instead of memorize these numbers, you can use constants instead (notice constants do not start with '$').
+ Data::Table::NUMBER
+ Data::Table::STRING
+ Data::Table::ASC
+ Data::Table::DESC</p>
+<p>Sorting is done in the priority of colID1, colID2, ...
+It returns 1 upon success or undef otherwise.
+Notice the table is rearranged as a result! This is different from perl's list sort, which returns a sorted copy while leave the original list untouched,
+the authors feel inplace sorting is more natural.</p>
+<p>table::sort can take a user supplied operator, this is useful when neither numerical nor alphabetic order is correct.</p>
+<pre>
+ $Well=["A_1", "A_2", "A_11", "A_12", "B_1", "B_2", "B_11", "B_12"];
+ $t = Data::Table->new([$Well], ["PlateWell"], 1);
+ $t->sort("PlateWell", 1, 0);
+ print join(" ", $t->col("PlateWell"));
+ # prints: A_1 A_11 A_12 A_2 B_1 B_11 B_12 B_2
+ # in string sorting, "A_11" and "A_12" appears before "A_2";
+ my $my_sort_func = sub {
+ my @a = split /_/, $_[0];
+ my @b = split /_/, $_[1];
+ my $res = ($a[0] cmp $b[0]) || (int($a[1]) <=> int($b[1]));
+ };
+ $t->sort("PlateWell", $my_sort_func, 0);
+ print join(" ", $t->col("PlateWell"));
+ # prints the correct order: A_1 A_2 A_11 A_12 B_1 B_2 B_11 B_12</pre>
+</dd>
+<dt><strong><a name="match_pattern" class="item">table table::match_pattern ($pattern, $countOnly)</a></strong></dt>
+
+<dd>
+<p>return a new table consisting those rows evaluated to be true by $pattern
+upon success or undef otherwise. If $countOnly is set to 1, it simply returns the number of rows that matches the string without making a new copy of table. $countOnly is 0 by default.</p>
+<p>Side effect: @Data::Table::OK (should use $t->{OK} after 1.62) stores a true/false array for the original table rows. Using it, users can find out what are the rows being selected/unselected.
+Side effect: @Data::Table::MATCH stores a reference to an array containing all row indices for matched rows.</p>
+<p>In the $pattern string, a column element should be referred as $_->[$colIndex]. E.g., match_pattern('$_->[0]>3 && $_->[1]=~/^L') retrieve all the rows where its first column is greater than 3 and second column starts with letter 'L'. Notice it only takes colIndex, column names are not acceptable here!</p>
+</dd>
+<dt><strong><a name="match_pattern_hash" class="item">table table::match_pattern_hash ($pattern, $countOnly)</a></strong></dt>
+
+<dd>
+<p>return a new table consisting those rows evaluated to be true by $pattern
+upon success or undef otherwise. If $countOnly is set to 1, it simply returns the number of rows that matches the string without making a new copy of table. $countOnly is 0 by default.</p>
+<p>Side effect: @Data::Table::OK stores a reference to a true/false array for the original table rows. Using it, users can find out what are the rows being selected/unselected.
+Side effect: @Data::Table::MATCH stores a reference to an array containing all row indices for matched rows.</p>
+<p>In the $pattern string, a column element should be referred as ${column_name}.
+<a href="#match_pattern_hash"><code>match_pattern_hash()</code></a> is added in 1.62. The difference between this method and match_pattern is each row is fed to the pattern as a hash %_.
+In the case of match_pattern, each row is fed as an array ref $_. The pattern for <a href="#match_pattern_hash"><code>match_pattern_hash()</code></a> becomes much cleaner.</p>
+<p>If a table has two columns: Col_A as the 1st column and Col_B as the 2nd column, a filter "Col_A>2 AND Col_<strong>2" is written before as
+ $t-</strong>match_pattern('$_->[0] > 2 && $_->[1] <2');
+where we need to figure out $t->colIndex('Col_A') is 0 and $t->colIndex('Col_B') is 1, in order to build the pattern.
+Now you can use column name directly in the pattern:
+ $t->match_pattern_hash('$_{Col_A} >2 && $_{Col_B} <2');
+This method creates $t->{OK}, as well as @Data::Table::OK, same as <a href="#match_pattern"><code>match_pattern()</code></a>.</p>
+</dd>
+<dt><strong><a name="match_string" class="item">table table::match_string ($s, $caseIgnore, $countOnly)</a></strong></dt>
+
+<dd>
+<p>return a new table consisting those rows contains string $s in any of its fields upon success, undef otherwise. if $caseIgnore evaluated to true, case will is be ignored (s/$s/i). If $countOnly is set to 1, it simply returns the number of rows that matches the string without making a new copy of table. $countOnly is 0 by default.</p>
+<p>Side effect: @Data::Table::OK stores a reference to a true/false array for the original table rows.
+Side effect: @Data::Table::MATCH stores a reference to an array containing all row indices for matched rows.
+Using it, users can find out what are the rows being selected/unselected.
+The $s string is actually treated as a regular expression and
+applied to each row element, therefore one can actually specify several keywords
+by saying, for instance, match_string('One|Other').</p>
+</dd>
+<dt><strong><a name="rowmask" class="item">table table::rowMask($mask, $complement)</a></strong></dt>
+
+<dd>
+<p>mask is reference to an array, where elements are evaluated to be true or false. The size of the mask must be equal to the nofRow of the table. return a new table consisting those rows where the corresponding mask element is true (or false, when complement is set to true).</p>
+<p>E.g., $t1=$tbl->match_string('keyword'); $t2=$tbl->rowMask(\@Data::Table::OK, 1) creates two new tables. $t1 contains all rows match 'keyword', while $t2 contains all other rows.</p>
+<p>mask is reference to an array, where elements are evaluated to be true or false. The size of the mask must be equal to the nofRow of the table. return
+ a new table consisting those rows where the corresponding mask element is true (or false, when complement is set to true).</p>
+<p>E.g., $t1=$tbl->match_string('keyword'); $t2=$tbl->rowMask(\@Data::Table::OK, 1) creates two new tables. $t1 contains all rows match 'keyword', while
+$t2 contains all other rows.</p>
+</dd>
+<dt><strong><a name="iterator" class="item">table table::iterator({$reverse => 0})</a></strong></dt>
+
+<dd>
+<p>Returns a reference to a enumerator routine, which enables one to loop through each table row. If $reverse is set to 1, it will enumerate backward. The convenience here is each row is fetch as a rowHashRef, so one can easily access row elements by name.</p>
+<pre>
+ my $next = $t_product->iterator();
+ while (my $row = $next->()) {
+ # have access to a row as a hash reference, access row number by &$next(1);
+ $t_product->setElm($next->(1), 'ProductName', 'New! '.$row->{ProductName});
+ }</pre>
+<p>In this example, each $row is fetched as a hash reference, so one can access the elements by $row->{colName}.
+Be aware that the elements in the hash is a copy of the original table elements, so modifying $row->{colName} does not modify the original table. If table modification is intended, one needs to obtain the row index of the returned row. $next->(1) call with a non-empty argument returns the row index of the record that was previously fetched with $next->(). In this example, one uses the row index to modify the original table.</p>
+</dd>
+<dt><strong><a name="each_group" class="item">table table::each_group($colsToGroupBy, $funsToApply)</a></strong></dt>
+
+<dd>
+<p>Primary key columns are specified in $colsToGroupBy. All rows are grouped by primary keys first (keys sorted as string). Then for each group, subroutines $funToAppy is applied to corresponding rows.
+$funToApply are passed with two parameters ($tableRef, $rowIDsRef). All rows sharing the key are passed in as a Data::Table object (with all columns and in the order of ascending row index) in the first parameter.
+The second optional parameter contains an array of row indices of the group members. Since all rows in the passed-in table contains the same keys, the key value can be obtained from its first table row.</p>
+</dd>
+<dt><strong><a name="group" class="item">table table::group($colsToGroupBy, $colsToCalculate, $funsToApply, $newColNames, $keepRestCols)</a></strong></dt>
+
+<dd>
+<p>Primary key columns are specified in $colsToGroupBy. All rows are grouped by primary keys first. Then for each group, an array of subroutines (in $funsToAppy) are applied to corresponding columns and yield a list of new columns (specified in $newColNames).</p>
+<p>$colsToGroupBy, $colsToCalculate are references to array of colIDs. $funsToApply is a reference to array of subroutine references. $newColNames are a
+reference to array of new column name strings. If specified, the size of arrays pointed by $colsToCalculate, $funsToApply and $newColNames should be i
+dentical. A column may be used more than once in $colsToCalculate.</p>
+<p>$keepRestCols is default to 1 (was introduced as 0 in 1.64, changed to 1 in 1.66 for backward compatibility) introduced in 1.64), otherwise, the remaining columns are returned with the first encountered value of that group.</p>
+<p>E.g., an employee salary table $t contains the following columns: Name, Sex, Department, Salary. (see examples in the SYNOPSIS)</p>
+<pre>
+ $t2 = $t->group(["Department","Sex"],["Name", "Salary"], [sub {scalar @_}, \&average], ["Nof Employee", "Average Salary"], 0);</pre>
+<p>Department, Sex are used together as the primary key columns, a new column "Nof Employee" is created by counting the number of employee names in each group, a new column "Average Salary" is created by averaging the Salary data falled into each group. As the result, we have the head count and average salary information for each (Department, Sex) pair. With your own functions (such as sum, product, average, standard deviation, etc), group method is very handy for acc [...]
+If primary key columns are not defined, all records will be treated as one group.</p>
+<pre>
+ $t2 = $t->group(undef,["Name", "Salary"], [sub {scalar @_}, \&average], ["Nof Employee", "Average Salary"], 0);</pre>
+<p>The above statement will output the total number of employees and their average salary as one line.</p>
+</dd>
+<dt><strong><a name="pivot" class="item">table table::pivot($colToSplit, $colToSplitIsStringOrNumeric, $colToFill, $colsToGroupBy, $keepRestCols)</a></strong></dt>
+
+<dd>
+<p>Every unique values in a column (specified by $colToSplit) become a new column. undef value become "NULL". $colToSplitIsStringOrNumeric is set to numeric (0 or Data::Table:NUMBER), the new column names are prefixed by "oldColumnName=". The new cell element is filled by the value specified by $colToFill (was 1/0 before version 1.63).</p>
+<p>Note: yes, it seems I made an incompatible change in version 1.64, where $colToSplitIsStringOrNumber used to be $colToSplitIsNumeric, where 0 meant STRING and 1 meant NUMBER. Now it is opposite. However, I also added auto-type detection code, that this parameter essentially is auto-guessed and most old code should behave the same as before.</p>
+<p>When primary key columns are specified by $colsToGroupBy, all records sharing the same primary key collapse into one row, with values in $colToFill filling the corresponding new columns. If $colToFill is not specified, a cell is filled with the number of records fall into that cell.</p>
+<p>$colToSplit and $colToFill are colIDs. $colToSplitIsNumeric is 1/0. $colsToGroupBy is a reference to array of colIDs. $keepRestCols is 1/0, by default is 0. If $keepRestCols is off, only primary key columns and new columns are exported, otherwise, all the rest columns are exported as well.</p>
+<p>E.g., applying pivot method to the resultant table of the example of the group method.</p>
+<pre>
+ $t2->pivot("Sex", 0, "Average Salary",["Department"]);</pre>
+<p>This creates a 2x3 table, where Departments are use as row keys, Sex (female and male) become two new columns. "Average Salary" values are used to fill the new table elements. Used together with group method, pivot method is very handy for accounting type of analysis.
+If $colsToGroupBy is left as undef, all rows are treated as one group. If $colToSplit is left as undef, the method will generate a column named "(all)" that matches all records share the corresponding primary key.</p>
+</dd>
+<dt><strong><a name="melt" class="item">table table::melt($keyCols, $variableCols, $arg_ref)</a></strong></dt>
+
+<dd>
+<p>The idea of <a href="#melt"><code>melt()</code></a> and <a href="#cast"><code>cast()</code></a> are taken from Hadley Wickham's Reshape package in R language.
+A table is first <a href="#melt"><code>melt()</code></a> into a tall-skiny format, where measurements are stored in the format of a variable-value pair per row.
+Such a format can then be easily <a href="#cast"><code>cast()</code></a> into various contingency tables.</p>
+<p>One needs to specify the columns consisting of primary keys, columns that are consider as variable columns. The output variable column is named 'variable' unless specified by $arg_ref{variableColName}. The output value column is named 'value', unless specified in $arg_ref{valueColName}. By default NULL values are not output, unless $arg_ref{skip_NULL} is set to false. By default empty string values are kept, unless one sets skip_empty to `.</p>
+<pre>
+ For each object (id), we measure variable x1 and x2 at two time points
+ $t = new Data::Table([[1,1,5,6], [1,2,3,5], [2,1,6,1], [2,2,2,4]], ['id','time','x1','x2'], Data::Table::ROW_BASED);
+ # id time x1 x2
+ # 1 1 5 6
+ # 1 2 3 5
+ # 2 1 6 1
+ # 2 2 2 4</pre>
+<pre>
+ # melting a table into a tall-and-skinny table
+ $t2 = $t->melt(['id','time']);
+ #id time variable value
+ # 1 1 x1 5
+ # 1 1 x2 6
+ # 1 2 x1 3
+ # 1 2 x2 5
+ # 2 1 x1 6
+ # 2 1 x2 1
+ # 2 2 x1 2
+ # 2 2 x2 4
+
+ # casting the table, &average is a method to calculate mean
+ # for each object (id), we calculate average value of x1 and x2 over time
+ $t3 = $t2->cast(['id'],'variable',Data::Table::STRING,'value', \&average);
+ # id x1 x2
+ # 1 4 5.5
+ # 2 4 2.5</pre>
+</dd>
+<dt><strong><a name="cast" class="item">table table::cast($colsToGroupBy, $colToSplit, $colToSplitIsStringOrNumeric, $colToCalculate, $funToApply)</a></strong></dt>
+
+<dd>
+<p>see <a href="#melt"><code>melt()</code></a>, as <a href="#melt"><code>melt()</code></a> and <a href="#cast"><code>cast()</code></a> are meant to use together.</p>
+<p>The table has been melten before. <a href="#cast"><code>cast()</code></a> group the table according to primary keys specified in $colsToGroupBy. For each group of objects sharing the same id,
+it further groups values (specified by $colToCalculate) according to unique variable names (specified by $colToSplit). Then it applies subroutine $funToApply to obtain an aggregate value.
+For the output, each unique primary key will be a row, each unique variable name will become a column, the cells are the calculated aggregated value.</p>
+<p>If $colsToGroupBy is undef, all rows are treated as within the same group. If $colToSplit is undef, a new column "(all)" is used to hold the results.</p>
+<pre>
+ $t = Data::Table->new( # create an employ salary table
+ [
+ ['Tom', 'male', 'IT', 65000],
+ ['John', 'male', 'IT', 75000],
+ ['Tom', 'male', 'IT', 65000],
+ ['John', 'male', 'IT', 75000],
+ ['Peter', 'male', 'HR', 85000],
+ ['Mary', 'female', 'HR', 80000],
+ ['Nancy', 'female', 'IT', 55000],
+ ['Jack', 'male', 'IT', 88000],
+ ['Susan', 'female', 'HR', 92000]
+ ],
+ ['Name', 'Sex', 'Department', 'Salary'], Data::Table::ROW_BASED);</pre>
+<pre>
+ # get a Department x Sex contingency table, get average salary across all four groups
+ print $t->cast(['Department'], 'Sex', Data::Table::STRING, 'Salary', \&average)->csv(1);
+ Department,female,male
+ IT,55000,73600
+ HR,86000,85000
+ # get average salary for each department
+ print $t->cast(['Department'], undef, Data::Table::STRING, 'Salary', \&average)->csv(1);
+ Department,(all)
+ IT,70500
+ HR,85666.6666666667</pre>
+<pre>
+ # get average salary for each gender
+ print $t->cast(['Sex'], undef, Data::Table::STRING, 'Salary', \&average)->csv(1);
+ Sex,(all)
+ male,75500
+ female,75666.6666666667
+
+ # get average salary for all records
+ print $t->cast(undef, undef, Data::Table::STRING, 'Salary', \&average)->csv(1);
+ (all)
+ 75555.5555555556</pre>
+</dd>
+</dl>
+<p>
+</p>
+<h2><a name="table_table_manipulations">Table-Table Manipulations</a></h2>
+<dl>
+<dt><strong><a name="rowmerge" class="item">int table::rowMerge ($tbl, $argRef)</a></strong></dt>
+
+<dd>
+<p>Append all the rows in the table object $tbl to the original rows.
+Before 1.62, the merging table $tbl must have the same number of columns as the original, as well as the columns are in exactly the same order.
+It returns 1 upon success, undef otherwise.
+The table object $tbl should not be used afterwards, since it becomes part of
+the new table.</p>
+<p>Since 1.62, you may provide {byName =>1, addNewCol=>1} as $argRef. If byName is set to 1, the columns in in $tbl do not need to be in the same order as they are in the first table, instead the column name is used for the matching. If addNewCol is set to 1, if $tbl contains a new column name that does not already exist in the first table, this new column will be automatically added to the resultant table. Typically, you want to specify there two options simultaneously.</p>
+</dd>
+<dt><strong><a name="colmerge" class="item">int table::colMerge ($tbl, $argRef)</a></strong></dt>
+
+<dd>
+<p>Append all the columns in table object $tbl to the original columns.
+Table $tbl must have the same number of rows as the original.
+It returns 1 upon success, undef otherwise.
+Table $tbl should not be used afterwards, since it becomes part of
+the new table.</p>
+<p>Since 1.62, you can specify {renameCol => 1} as $argRef. This is to auto fix any column name collision. If $tbl contains a column that already exists in the first table, it will be renamed (by a suffix _2) to avoid the collision.</p>
+</dd>
+<dt><strong><a name="join" class="item">table table::join ($tbl, $type, $cols1, $cols2, $argRef)</a></strong></dt>
+
+<dd>
+<p>Join two tables. The following join types are supported (defined by $type):</p>
+<p>0: inner join
+1: left outer join
+2: right outer join
+3: full outer join</p>
+<p>In 1.62, instead of memorize these numbers, you can use constants instead (notice constants do not start with '$').
+ Data::Table::INNER_JOIN
+ Data::Table::LEFT_JOIN
+ Data::Table::RIGHT_JOIN
+ Data::Table::FULL_JOIN</p>
+<p>$cols1 and $cols2 are references to array of colIDs, where rows with the same elements in all listed columns are merged. As the result table, columns listed in $cols2 are deleted, before a new table is returned.</p>
+<p>The implementation is hash-join, the running time should be linear with respect to the sum of number of rows in the two tables (assume both tables fit in memory).</p>
+<p>If the non-key columns of the two tables share the same name, the routine will fail, as the result table cannot contain two columns of the same name. In 1.62, one can specify {renameCol=>1} as $argRef, so that the second column will be automatically renamed (with suffix _2) to avoid collision.</p>
+<p>If you would like to treat the NULLs in the key columns as empty string, set {NULLasEmpty => 1}. If you do not want to treat NULLs as empty strings, but you still like the NULLs in two tables to be considered as equal (but not equal to ''), set {matchNULL => 1}. Obviously if NULLasEmpty is set to 1, matchNULL will have no effect.</p>
+</dd>
+</dl>
+<p>
+</p>
+<h2><a name="internal_methods">Internal Methods</a></h2>
+<p>All internal methods are mainly implemented for used by
+other methods in the Table class. Users should avoid using them.
+Nevertheless, they are listed here for developers who
+would like to understand the code and may derive a new class from Data::Table.</p>
+<dl>
+<dt><strong><a name="int_table_rotate" class="item">int table::rotate</a></strong></dt>
+
+<dd>
+<p>convert the internal structure of a table between row-based and column-based.
+return 1 upon success, undef otherwise.</p>
+</dd>
+<dt><strong><a name="csvescape" class="item">string csvEscape($string, {delimiter=>, qualifier})</a></strong></dt>
+
+<dd>
+<p>Encode a scalar into a CSV-formatted field.</p>
+<p>optional named arguments: delimiter and qualifier, in case user wants to use characters other than the defaults.
+The default delimiter and qualifier is taken from $Data::Table::DEFAULTS{'CSV_DELIMITER'} (defaults to ',') and $Data::Table::DEFAULTS{'CSV_QUALIFIER'} (defaults to '"'), respectively.</p>
+<p>Please note that this function only escape one element in a table. To escape the whole table row, you need to
+ join($delimiter, map {csvEscape($_)} @row . $endl;
+$endl refers to End-of-Line, which you may or may not want to add, and it is OS-dependent. Therefore, csvEscape method is kept to the simplest form as an element transformer.</p>
+</dd>
+<dt><strong><a name="parsecsv" class="item">refto_array <code>parseCSV($string)</code></a></strong></dt>
+
+<dd>
+<p>Break a CSV encoded string to an array of scalars (check it out, we did it the cool way).</p>
+<p>optional argument size: specify the expected number of fields after csv-split.
+optional named arguments: delimiter and qualifier, in case user wants to use characters other than the defaults.
+respectively. The default delimiter and qualifier is taken from $Data::Table::DEFAULTS{'CSV_DELIMITER'} (defaults to ',') and $Data::Table::DEFAULTS{'CSV_QUALIFIER'} (defaults to '"'), respectively.</p>
+</dd>
+<dt><strong><a name="tsvescape" class="item">string <code>tsvEscape($rowRef)</code></a></strong></dt>
+
+<dd>
+<p>Encode a scalar into a TSV-formatted string.</p>
+</dd>
+</dl>
+<p>
+</p>
+<hr />
+<h1><a name="tsv_format">TSV FORMAT</a></h1>
+<p>There is no standard for TSV format as far as we know. CSV format can't handle binary data very well, therefore, we choose the TSV format to overcome this limitation.</p>
+<p>We define TSV based on MySQL convention.</p>
+<pre>
+ "\0", "\n", "\t", "\r", "\b", "'", "\"", and "\\" are all escaped by '\' in the TSV file.
+ (Warning: MySQL treats '\f' as 'f', and it's not escaped here)
+ Undefined values are represented as '\N'.</pre>
+<p>However, you can switch off this transformation by setting {transform_element => 0} in the fromTSV or tsv method.
+Before if a cell reads 'A line break is \n', it is read in as 'A link break is [return]' in memory. When use tsv method to export, it is transformed back to 'A line break is \n'. However, if it is exported as a csv, the [return] will break the format. Now if transform_element is set to 0, the cell is stored as 'A line break is \n' in memory, so that csv export will be correct. However, do remember to set {transform_element => 0} in tsv export method, otherwise, the cell will become 'A [...]
+<p>
+</p>
+<hr />
+<h1><a name="interface_to_other_softwares">INTERFACE TO OTHER SOFTWARES</a></h1>
+<p>Spreadsheet is a very generic type, therefore Data::Table class provides an easy
+interface between databases, web pages, CSV/TSV files, graphics packages, etc.</p>
+<p>Here is a summary (partially repeat) of some classic usages of Data::Table.</p>
+<p>
+</p>
+<h2><a name="interface_to_database_and_web">Interface to Database and Web</a></h2>
+<pre>
+ use DBI;</pre>
+<pre>
+ $dbh= DBI->connect("DBI:mysql:test", "test", "") or die $DBI::errstr;
+ my $minAge = 10;
+ $t = Data::Table::fromSQL($dbh, "select * from mytable where age >= ?", [$minAge]);
+ print $t->html;</pre>
+<p>
+</p>
+<h2><a name="interface_to_csv_tsv">Interface to CSV/TSV</a></h2>
+<pre>
+ $t = fromFile("mydata.csv"); # after version 1.51
+ $t = fromFile("mydata.tsv"); # after version 1.51</pre>
+<pre>
+ $t = fromCSV("mydata.csv");
+ $t->sort(1,1,0);
+ print $t->csv;</pre>
+<pre>
+ Same for TSV</pre>
+<p>
+</p>
+<h2><a name="interface_to_excel_xls_xlsx">Interface to Excel XLS/XLSX</a></h2>
+<p>Read in two tables from NorthWind.xls file, writes them out to XLSX format. See Data::Table::Excel module for details.</p>
+<pre>
+ use Data::Table::Excel;</pre>
+<pre>
+ my ($tableObjects, $tableNames)=xls2tables("NorthWind.xls");
+ $t_category = $tableObjects[0];
+ $t_product = $tableObjects[1];</pre>
+<pre>
+ tables2xlsx("NorthWind.xlsx", [$t_category, $t_product]);</pre>
+<p>
+</p>
+<h2><a name="interface_to_graphics_package">Interface to Graphics Package</a></h2>
+<pre>
+ use GD::Graph::points;</pre>
+<pre>
+ $graph = GD::Graph::points->new(400, 300);
+ $t2 = $t->match('$_->[1] > 20 && $_->[3] < 35.7');
+ my $gd = $graph->plot($t->colRefs([0,2]));
+ open(IMG, '>mygraph.png') or die $!;
+ binmode IMG;
+ print IMG $gd->png;
+ close IMG;</pre>
+<p>
+</p>
+<hr />
+<h1><a name="author">AUTHOR</a></h1>
+<p>Copyright 1998-2008, Yingyao Zhou & Guangzhou Zou. All rights reserved.</p>
+<p>It was first written by Zhou in 1998, significantly improved and maintained by Zou since 1999. The authors thank Tong Peng and Yongchuang Tao for valuable suggestions. We also thank those who kindly reported bugs, some of them are acknowledged in the "Changes" file.</p>
+<p>This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.</p>
+<p>Please send bug reports and comments to: easydatabase at gmail dot com. When sending
+bug reports, please provide the version of Table.pm, the version of
+Perl.</p>
+<p>
+</p>
+<hr />
+<h1><a name="see_also">SEE ALSO</a></h1>
+<pre>
+ DBI, GD::Graph, Data::Table::Excel.
+
+</pre>
+
+</body>
+
+</html>
diff --git a/Table.pm b/Table.pm
new file mode 100755
index 0000000..b4c83e3
--- /dev/null
+++ b/Table.pm
@@ -0,0 +1,3268 @@
+package Data::Table;
+BEGIN { die "Your perl version is old, see README for instructions" if $] < 5.005; }
+
+use strict;
+use vars qw($VERSION %DEFAULTS);
+use Carp;
+#use Data::Dumper;
+
+$VERSION = '1.72';
+%DEFAULTS = (
+ "CSV_DELIMITER"=>',', # controls how to read/write CSV file
+ "CSV_QUALIFIER"=>'"',
+ "OS"=>0,
+ # operatoring system: 0 for UNIX (\n as linebreak), 1 for Windows
+ # (\r\n as linebreak), 2 for MAC (\r as linebreak)
+ # this controls how to read and write CSV/TSV file
+ "ENCODING"=>'UTF-8'
+ # default encoding for fromFile, fromCSV, fromTSV
+);
+%Data::Table::TSV_ESC = ( '0'=>"\0", 'n'=>"\n", 't'=>"\t", 'r'=>"\r", 'b'=>"\b",
+ "'"=>"'", '"'=>"\"", '\\'=>"\\" );
+%Data::Table::TSV_ENC = ( "\0"=>'0', "\n"=>'n', "\t"=>'t', "\r"=>'r', "\b"=>'b',
+ "'"=>"'", "\""=>'"', "\\"=>'\\' );
+use constant ROW_BASED => 0;
+use constant COL_BASED => 1;
+use constant NUMBER => 0;
+use constant STRING => 1;
+use constant ASC => 0;
+use constant DESC => 1;
+use constant INNER_JOIN => 0;
+use constant LEFT_JOIN => 1;
+use constant RIGHT_JOIN => 2;
+use constant FULL_JOIN => 3;
+use constant OS_UNIX => 0;
+use constant OS_PC => 1;
+use constant OS_MAC => 2;
+
+sub new {
+ my ($pkg, $data, $header, $type, $enforceCheck) = @_;
+ my $class = ref($pkg) || $pkg;
+ $type = 0 unless defined($type);
+ $header=[] unless defined($header);
+ $data=[] unless defined($data);
+ $enforceCheck = 1 unless defined($enforceCheck);
+ confess "new Data::Table: Size of data does not match header\n"
+ if (($type && (scalar @$data) && $#{$data} != $#{$header}) ||
+ (!$type && (scalar @$data) && $#{$data->[0]} != $#{$header}));
+ my $colHash = checkHeader($header);
+ if ($enforceCheck && scalar @$data > 0) {
+ my $size=scalar @{$data->[0]};
+ for (my $j =1; $j<scalar @$data; $j++) {
+ confess "Inconsistent array size at data[$j]" unless (scalar @{$data->[$j]} == $size);
+ }
+ } elsif (scalar @$data == 0) {
+ $type = 0;
+ }
+ my $self={ data=>$data, header=>$header, type=>$type, colHash=>$colHash, OK=>[], MATCH=>[]};
+ return bless $self, $class;
+}
+
+sub checkHeader {
+ my $header = shift;
+ my $colHash = {};
+ for (my $i = 0; $i < scalar @$header; $i++) {
+ my $elm = $header->[$i];
+ #warn "Column name: $elm at column ".($i+1)." is an integer, using an integer column name will mask the corresponding column index!" if ($elm =~ /^\d+$/);
+ confess "Undefined column name (empty or all space) at column ".($i+1) unless $elm;
+ #confess "Header name ".$colHash->{$elm}." appears more than once" if defined($colHash->{$elm});
+ if (defined($colHash->{$elm})) {
+ confess "Header name ($elm) appears more than once: in column ".($colHash->{$elm}+1)." and column ".($i+1).".";
+ }
+ $colHash->{$elm} = $i;
+ }
+ return $colHash;
+}
+
+# translate a column name into its position in the header
+# (also in column-based table)
+sub colIndex {
+ my ($self, $colID) = @_;
+ return $self->{colHash}->{$colID} if exists $self->{colHash}->{$colID};
+ return $colID if $colID =~ /^\d+$/;
+ return -1;
+ #if ($colID =~ /\D/) {
+ # my $i = $self->{colHash}->{$colID};
+ # return -1 unless defined($i);
+ # return $i;
+ #}
+ #return $colID; # assume an index already
+}
+
+sub hasCol {
+ my ($self, $col) = @_;
+ return $self->colIndex($col) >= 0;
+}
+
+sub nofCol {
+ my $self = shift;
+ return scalar @{$self->{header}};
+}
+
+sub isEmpty {
+ my $self = shift;
+ return $self->nofCol == 0;
+}
+
+sub nofRow {
+ my $self = shift;
+ return 0 if (scalar @{$self->{data}} == 0);
+ return ($self->{type})?
+ scalar @{$self->{data}->[0]} : scalar @{$self->{data}};
+}
+
+sub lastRow {
+ my $self = shift;
+ return $self->nofRow - 1;
+}
+
+sub lastCol {
+ my $self = shift;
+ return $self->nofCol - 1;
+}
+
+sub colName {
+ my ($self, $colNumericIndex) = @_;
+ return ($self->header())[$colNumericIndex];
+}
+
+sub iterator {
+ my ($self, $arg_ref) = @_;
+ my %arg = defined $arg_ref ? %$arg_ref : ();
+ $arg{reverse} = 0 unless exists $arg{reverse};
+ my $current_row = $arg{reverse} ? $self->lastRow : 0;
+
+ return sub {
+ my $rowIdx = shift;
+ if (defined $rowIdx) { # return row index for previously returned record
+ my $prevRow = $arg{reverse} ? $current_row+1 : $current_row-1;
+ return ($prevRow<0 or $prevRow > $self->nofRow-1)? undef: $prevRow;
+ }
+ return undef if $current_row < 0 or $current_row > $self->nofRow - 1;
+ my $oldRow = $current_row;
+ $arg{reverse} ? $current_row-- : $current_row++;
+ return $self->rowHashRef($oldRow);
+ }
+}
+
+# still need to consider quotes and comma in string
+# need to get csv specification
+sub csvEscape {
+ my ($s, $arg_ref) = @_;
+ my ($delimiter, $qualifier) = ($Data::Table::DEFAULTS{CSV_DELIMITER}, $Data::Table::DEFAULTS{CSV_QUALIFIER});
+ $delimiter = $arg_ref->{'delimiter'} if (defined($arg_ref) && defined($arg_ref->{'delimiter'}));
+ $qualifier = $arg_ref->{'qualifier'} if (defined($arg_ref) && defined($arg_ref->{'qualifier'}));
+ return '' unless defined($s);
+ my $qualifier2 = $qualifier;
+ $qualifier2 = substr($qualifier, 1, 1) if length($qualifier)>1; # in case qualifier is a special symbol for regular expression
+ $s =~ s/$qualifier/$qualifier2$qualifier2/g;
+ if ($s =~ /[$qualifier$delimiter\r\n]/) { return "$qualifier2$s$qualifier2"; }
+ return $s;
+}
+
+sub tsvEscape {
+ my $s = shift;
+ #my %ESC = ( "\0"=>'0', "\n"=>'n', "\t"=>'t', "\r"=>'r', "\b"=>'b',
+ # "'"=>"'", "\""=>'"', "\\"=>'\\' );
+ ## what about \f? MySQL treats \f as f.
+ return "\\N" unless defined($s);
+ $s =~ s/([\0\\\b\r\n\t"'])/\\$Data::Table::TSV_ENC{$1}/g;
+ return $s;
+}
+
+# output table in CSV format
+sub csv {
+ my ($self, $header, $arg_ref)=@_;
+ my ($status, @t);
+ my $s = '';
+ my ($OS, $fileName_or_handler) = ($Data::Table::DEFAULTS{OS}, undef);
+ $OS = $arg_ref->{'OS'} if (defined($arg_ref) && defined($arg_ref->{'OS'}));
+ my ($delimiter, $qualifier) = ($Data::Table::DEFAULTS{CSV_DELIMITER}, $Data::Table::DEFAULTS{CSV_QUALIFIER});
+ if (defined($arg_ref)) {
+ $delimiter = $arg_ref->{'delimiter'} if defined($arg_ref->{'delimiter'});
+ $qualifier = $arg_ref->{'qualifier'} if defined($arg_ref->{'qualifier'});
+ $fileName_or_handler = $arg_ref->{'file'} if defined($arg_ref->{'file'});
+ }
+ my $delimiter2 = $delimiter; $delimiter2 = substr($delimiter, 1, 1) if length($delimiter)>1;
+ my $endl = ($OS==2)?"\r":(($OS==1)?"\r\n":"\n");
+ $header=1 unless defined($header);
+ $s=join($delimiter2, map {csvEscape($_, {delimiter=>$delimiter, qualifier=>$qualifier})} @{$self->{header}}) . $endl if $header;
+###### $self->rotate if $self->{type};
+ if ($self->{data}) {
+ $self->rotate() if ($self->{type});
+ my $data=$self->{data};
+ for (my $i=0; $i<=$#{$data}; $i++) {
+ $s .= join($delimiter2, map {csvEscape($_, {delimiter=>$delimiter, qualifier=>$qualifier})} @{$data->[$i]}) . $endl;
+ }
+ }
+ if (defined($fileName_or_handler)) {
+ my $OUT;
+ my $isFileHandler = ref($fileName_or_handler) ne '';
+ if ($isFileHandler) {
+ $OUT = $fileName_or_handler;
+ } else {
+ open($OUT, "> $fileName_or_handler") or confess "Cannot open $fileName_or_handler to write.\n";
+ binmode $OUT;
+ }
+ print $OUT $s;
+ close($OUT) unless $isFileHandler;
+ }
+ return $s;
+}
+
+# output table in TSV format
+sub tsv {
+ my ($self, $header, $arg_ref)=@_;
+ my ($status, @t);
+ my $s = '';
+ my ($OS, $fileName_or_handler, $transform_element) = ($Data::Table::DEFAULTS{OS}, undef, 1);
+ if (defined($arg_ref)) {
+ $OS = $arg_ref->{'OS'} if (defined($arg_ref->{'OS'}));
+ $fileName_or_handler = $arg_ref->{'file'} if (defined($arg_ref->{'file'}));
+ $transform_element = $arg_ref->{'transform_element'} if (defined($arg_ref->{'transform_element'}));
+ }
+ my $endl = ($OS==2)?"\r":(($OS==1)?"\r\n":"\n");
+ $header=1 unless defined($header);
+ if ($header) {
+ if ($transform_element) {
+ $s=join("\t", map {tsvEscape($_)} @{$self->{header}}) . $endl;
+ } else {
+ $s=join("\t",@{$self->{header}}) . $endl;
+ }
+ }
+###### $self->rotate if $self->{type};
+ if ($self->{data}) {
+ $self->rotate() if ($self->{type});
+ my $data=$self->{data};
+ for (my $i=0; $i<=$#{$data}; $i++) {
+ if ($transform_element) {
+ $s .= join("\t", map {tsvEscape($_)} @{$data->[$i]}) . $endl;
+ } else {
+ $s .= join("\t", @{$data->[$i]}) . $endl;
+ }
+ }
+ }
+ if (defined($fileName_or_handler)) {
+ my $OUT;
+ my $isFileHandler = ref($fileName_or_handler) ne '';
+ if ($isFileHandler) {
+ $OUT = $fileName_or_handler;
+ } else {
+ open($OUT, "> $fileName_or_handler") or confess "Cannot open $fileName_or_handler to write.\n";
+ binmode $OUT;
+ }
+ print $OUT $s;
+ close($OUT) unless $isFileHandler;;
+ }
+ return $s;
+}
+
+# output table in HTML format
+sub html {
+ my ($self, $colorArrayRef_or_classHashRef, $tag_tbl, $tag_tr, $tag_th, $tag_td, $portrait) = @_;
+ my ($s, $s_tr, $s_td, $s_th) = ("", "tr", "", "th");
+ my $key;
+ $tag_tbl = { class => "data_table" } unless (ref $tag_tbl eq 'HASH');
+ $tag_tr = {} unless (ref $tag_tr eq 'HASH');
+ $tag_th = {} unless (ref $tag_th eq 'HASH');
+ $tag_td = {} unless (ref $tag_td eq 'HASH');
+ $portrait = 1 unless defined($portrait);
+
+ $s = "<table";
+ foreach $key (keys %$tag_tbl) {
+ $s .= " $key=\"$tag_tbl->{$key}\"";
+ }
+ $s .= ">\n";
+ $s .= "<thead>\n" if ($portrait);
+ my $header=$self->{header};
+ my $l_colorByClass = 0;
+ my @BG_COLOR=("#D4D4BF","#ECECE4","#CCCC99");
+ my @CELL_CLASSES=("data_table_odd","data_table_even","data_table_header");
+ if (ref($colorArrayRef_or_classHashRef) eq "HASH") {
+ $l_colorByClass = 1;
+ $CELL_CLASSES[1]=$colorArrayRef_or_classHashRef->{even} if defined($colorArrayRef_or_classHashRef->{even});
+ $CELL_CLASSES[0]=$colorArrayRef_or_classHashRef->{odd} if defined($colorArrayRef_or_classHashRef->{odd});
+ $CELL_CLASSES[2]=$colorArrayRef_or_classHashRef->{header} if defined($colorArrayRef_or_classHashRef->{header});
+ } elsif ((ref($colorArrayRef_or_classHashRef) eq "ARRAY") && (scalar @$colorArrayRef_or_classHashRef==3)) {
+ @BG_COLOR=@$colorArrayRef_or_classHashRef;
+ }
+ foreach $key (keys %$tag_tr) {
+ $s_tr .= " $key=\"$tag_tr->{$key}\"";
+ }
+ foreach $key (keys %$tag_th) {
+ $s_th .= " $key=\"$tag_th->{$key}\"";
+ }
+ if ($portrait) {
+ $s .= "<$s_tr ".($l_colorByClass? ("class=\"".$CELL_CLASSES[2]."\""):("bgcolor=\"".$BG_COLOR[2]."\""))."><$s_th>".
+ join("</th><$s_th>", @$header) . "</th></tr>\n";
+ $s .= "</thead>\n";
+ $self->rotate() if $self->{type};
+ my $data=$self->{data};
+ $s .= "<tbody>\n";
+ for (my $i=0; $i<=$#{$data}; $i++) {
+ $s .= "<$s_tr ".($l_colorByClass? ("class=\"".$CELL_CLASSES[$i%2]."\""):("bgcolor=\"".$BG_COLOR[$i%2]."\"")).">";
+ for (my $j=0; $j<=$#{$header}; $j++) {
+ my $s_td = $tag_td->{$j} || $tag_td->{$header->[$j]};
+ $s .= defined($s_td)? "<td $s_td>":"<td>";
+ $s .= (defined($data->[$i][$j]) && $data->[$i][$j] ne '')?$data->[$i][$j]:" ";
+ $s .= "</td>";
+ }
+ $s .= "</tr>\n";
+ }
+ $s .= "</tbody>\n";
+ } else {
+ $self->rotate() unless $self->{type};
+ my $data=$self->{data};
+ for (my $i = 0; $i <= $#{$header}; $i++) {
+ $s .= "<$s_tr><$s_th ".($l_colorByClass? ("class=\"".$CELL_CLASSES[2]."\""):("bgcolor=\"".$BG_COLOR[2]."\"")).">".
+ $header->[$i] . "</th>";
+ my $s_td = $tag_td->{$i} || $tag_td->{$header->[$i]};
+ for (my $j=0; $j<=$#{$data->[0]}; $j++) {
+ $s .= defined($s_td)? "<td $s_td":"<td";
+ $s .= ($l_colorByClass?(" class=\"".$CELL_CLASSES[$j%2]."\""):(" bgcolor=\"".$BG_COLOR[$j%2]."\"")).">";
+ $s .= (defined($data->[$i][$j]) && $data->[$i][$j] ne '')?$data->[$i][$j]:' ';
+ $s .= "</td>";
+ }
+ $s .= "</tr>\n";
+ }
+ }
+ $s .= "</table>\n";
+ return $s;
+}
+
+# output table in HTML format, with table orientation rotated,
+# so that each HTML table row is a column in the table
+# This is useful for a slim table (few columns but many rows)
+sub html2 {
+ my ($self, $colorArrayRef_or_classHashRef, $tag_tbl, $tag_tr, $tag_th, $tag_td) = @_;
+ return $self->html($colorArrayRef_or_classHashRef, $tag_tbl, $tag_tr, $tag_th, $tag_td, 0);
+}
+
+# apply a $fun to each elm in a col
+# function only has access to one element per row
+sub colMap {
+ my ($self, $colID, $fun) = @_;
+ my $c=$self->checkOldCol($colID);
+ return undef unless defined $c;
+ $self->rotate() unless $self->{type};
+ my $ref = $self->{data}->[$c];
+ my @tmp = map {scalar $fun->($_)} @$ref;
+ $self->{data}->[$c] = \@tmp;
+ return 1;
+}
+
+# apply a $fun to each row in the table
+# function has access to all elements in that row
+sub colsMap {
+ my ($self, $fun) = @_;
+ $self->rotate() if $self->{type};
+ map {&$fun} @{$self->{data}};
+ return 1;
+}
+
+sub addRow {
+ my ($self, $rowRef, $rowIdx, $arg_ref) = @_;
+ my %arg = defined $arg_ref ? %$arg_ref : ();
+ $arg{addNewCol} = 0 unless exists $arg{addNewCol};
+
+ my $numRow=$self->nofRow();
+ my @t;
+ my $myRowRef = $rowRef;
+
+ if ($arg{addNewCol}) {
+ if (ref $myRowRef eq 'HASH') {
+ foreach my $key (keys %$myRowRef) {
+ next if $self->colIndex($key) >= 0;
+ my @col = (undef) x $self->nofRow;
+ $self->addCol(\@col, $key);
+ }
+ } elsif (ref $myRowRef eq 'ARRAY') {
+ for (my $i=$self->nofCol; $i< scalar @$myRowRef; $i++) {
+ my @col = (undef) x $self->nofRow;
+ $self->addCol(\@col, "col".($i+1));
+ }
+ }
+ }
+
+ if (ref $myRowRef eq 'HASH') {
+ if ($self->isEmpty) {
+ my $i = 0;
+ foreach my $s (keys %$myRowRef) {
+ push @{$self->{header}}, $s;
+ $self->{colHash}->{$s} = $i++;
+ }
+ }
+ my @one = ();
+ my @header = $self->header;
+ for (my $i=0; $i< scalar @header; $i++) {
+ $one[$i] = $myRowRef->{$header[$i]};
+ }
+ $myRowRef = \@one;
+ } elsif (ref $myRowRef eq 'ARRAY') {
+ confess "addRow: size of added row does not match those in the table\n"
+ if scalar @$myRowRef != $self->nofCol();
+ } else {
+ confess "addRow: parameter rowRef has to be either an array_ref or a hash_ref\n";
+ }
+ $rowIdx=$numRow unless defined($rowIdx);
+ return undef unless defined $self->checkNewRow($rowIdx);
+ $self->rotate() if $self->{type};
+ my $data=$self->{data};
+ if ($rowIdx == 0) {
+ unshift @$data, $myRowRef;
+ } elsif ($rowIdx == $numRow) {
+ push @$data, $myRowRef;
+ } else {
+ @t = splice @$data, $rowIdx;
+ push @$data, $myRowRef, @t;
+ }
+ return 1;
+}
+
+sub delRow {
+ my ($self, $rowIdx ) = @_;
+ return undef unless defined $self->checkOldRow($rowIdx);
+ $self->rotate() if $self->{type};
+ my $data=$self->{data};
+ my @dels=splice(@$data, $rowIdx, 1);
+ return shift @dels;
+}
+
+sub delRows {
+ my ($self, $rowIdcsRef) = @_;
+ my $rowIdx;
+ $self->rotate() if $self->{type};
+ my @dels = @{$self->{data}}[@$rowIdcsRef];
+ my @indices = sort { $b <=> $a } @$rowIdcsRef;
+ #my @dels=();
+ foreach $rowIdx (@indices) {
+ #push @dels, $self->delRow($rowIdx);
+ $self->delRow($rowIdx);
+ }
+ return @dels;
+}
+
+# append a column to the table, input is a referenceof_array
+
+sub addCol {
+ my ($self, $colRef, $colName, $colIdx) = @_;
+ my $numCol=$self->nofCol();
+ my @t;
+ if (!defined($colRef) || ref($colRef) eq '') {
+ # fill the new column with $colRef as the default value
+ my @col = ($colRef) x $self->nofRow;
+ $colRef = \@col;
+ } else {
+ confess "addCol: size of added col does not match rows in the table\n"
+ if @$colRef != $self->nofRow() and $numCol > 0;
+ }
+ $colIdx=$numCol unless defined($colIdx);
+ return undef unless defined $self->checkNewCol($colIdx, $colName);
+ $self->rotate() unless $self->{type};
+ my $data=$self->{data};
+ my $header=$self->{header};
+ if ($colIdx == 0) {
+ unshift @$header, $colName;
+ } elsif ($colIdx == $numCol) {
+ push @$header, $colName;
+ } else {
+ @t = splice @$header, $colIdx;
+ push @$header, $colName, @t;
+ }
+
+ if ($colIdx == 0) {
+ unshift @$data, $colRef;
+ } elsif ($colIdx == $numCol) {
+ push @$data, $colRef;
+ } else {
+ @t = splice @$data, $colIdx;
+ push @$data, $colRef, @t;
+ }
+
+ for (my $i = 0; $i < scalar @$header; $i++) {
+ my $elm = $header->[$i];
+ $self->{colHash}->{$elm} = $i;
+ }
+ return 1;
+}
+
+sub delCol {
+ my ($self, $colID) = @_;
+ my $c=$self->checkOldCol($colID);
+ return undef unless defined $c;
+ $self->rotate() unless $self->{type};
+ my $header=$self->{header};
+ my $name=$self->{header}->[$c];
+ splice @$header, $c, 1;
+ my $data=$self->{data};
+ my @dels=splice @$data, $c, 1;
+ delete $self->{colHash}->{$name};
+ for (my $i = $c; $i < scalar @$header; $i++) {
+ my $elm = $header->[$i];
+ $self->{colHash}->{$elm} = $i;
+ }
+ return shift @dels;
+}
+
+sub delCols {
+ my ($self, $colIDsRef) = @_;
+ my $idx;
+ my @indices = map { $self->colIndex($_) } @$colIDsRef;
+ $self->rotate() unless $self->{type};
+ my @dels = @{$self->{data}}[@indices];
+ @indices = sort { $b <=> $a } @indices;
+ #my @dels=();
+ foreach my $colIdx (@indices) {
+ $self->delCol($colIdx);
+ }
+ return @dels;
+}
+
+
+sub rowRef {
+ my ($self, $rowIdx) = @_;
+ return undef unless defined $self->checkOldRow($rowIdx);
+ $self->rotate if $self->{type};
+ return $self->{data}->[$rowIdx];
+}
+
+sub rowRefs {
+ my ($self, $rowIdcsRef) = @_;
+ $self->rotate if $self->{type};
+ return $self->{data} unless defined $rowIdcsRef;
+ my @ones = ();
+ my $rowIdx;
+ foreach $rowIdx (@$rowIdcsRef) {
+ push @ones, $self->rowRef($rowIdx);
+ }
+ return \@ones;
+}
+
+sub row {
+ my ($self, $rowIdx) = @_;
+ my $data = $self->{data};
+ return undef unless defined $self->checkOldRow($rowIdx);
+ if ($self->{type}) {
+ my @one=();
+ for (my $i = 0; $i < scalar @$data; $i++) {
+ push @one, $data->[$i]->[$rowIdx];
+ }
+ return @one;
+ } else {
+ return @{$data->[$rowIdx]};
+ }
+}
+
+sub rowHashRef {
+ my ($self, $rowIdx) = @_;
+ my $data = $self->{data};
+ return undef unless defined $self->checkOldRow($rowIdx);
+ my $header=$self->{header};
+ my $one = {};
+ for (my $i = 0; $i < scalar @$header; $i++) {
+ $one->{$header->[$i]} = ($self->{type})?
+ $self->{data}->[$i]->[$rowIdx]:$self->{data}->[$rowIdx]->[$i];
+ }
+ return $one;
+}
+
+sub colRef {
+ my ($self, $colID) = @_;
+ my $c=$self->checkOldCol($colID);
+ return undef unless defined $c;
+ $self->rotate() unless $self->{type};
+ return $self->{data}->[$c];
+}
+
+sub colRefs {
+ my ($self, $colIDsRef) = @_;
+ $self->rotate unless $self->{type};
+ return $self->{data} unless defined $colIDsRef;
+ my @ones = ();
+ my $colID;
+ foreach $colID (@$colIDsRef) {
+ push @ones, $self->colRef($colID);
+ }
+ return \@ones;
+}
+
+sub col {
+ my ($self, $colID) = @_;
+ my $data = $self->{data};
+ my $c=$self->checkOldCol($colID);
+ return undef unless defined $c;
+ if (!$self->{type}) {
+ my @one=();
+ for (my $i = 0; $i < scalar @$data; $i++) {
+ push @one, $data->[$i]->[$c];
+ }
+ return @one;
+ } else {
+ return () unless ref($data->[$c]) eq "ARRAY";
+ return @{$data->[$c]};
+ }
+}
+
+sub rename {
+ my ($self, $colID, $name) = @_;
+ my $oldName;
+ my $c=$self->checkOldCol($colID);
+ return undef unless defined $c;
+ $oldName=$self->{header}->[$c];
+ return if ($oldName eq $name);
+ return undef unless defined $self->checkNewCol($c, $name);
+ $self->{header}->[$c]=$name;
+ # $self->{colHash}->{$oldName}=undef; # undef still keeps the entry, use delete instead!
+ delete $self->{colHash}->{$oldName};
+ $self->{colHash}->{$name}=$c;
+ return 1;
+}
+
+sub replace{
+ my ($self, $oldColID, $newColRef, $newName) = @_;
+ my $oldName;
+ my $c=$self->checkOldCol($oldColID);
+ return undef unless defined $c;
+ $oldName=$self->{header}->[$c];
+ $newName=$oldName unless defined($newName);
+ unless ($oldName eq $newName) {
+ return undef unless defined $self->checkNewCol($c, $newName);
+ }
+ confess "New column size ".(scalar @$newColRef)." must be ".$self->nofRow() unless (scalar @$newColRef==$self->nofRow());
+ $self->rename($c, $newName);
+ $self->rotate() unless $self->{type};
+ my $old=$self->{data}->[$c];
+ $self->{data}->[$c]=$newColRef;
+ return $old;
+}
+
+sub swap{
+ my ($self, $colID1, $colID2) = @_;
+ my $c1=$self->checkOldCol($colID1);
+ return undef unless defined $c1;
+ my $c2=$self->checkOldCol($colID2);
+ return undef unless defined $c2;
+ my $name1=$self->{header}->[$c1];
+ my $name2=$self->{header}->[$c2];
+
+ $self->{header}->[$c1]=$name2;
+ $self->{header}->[$c2]=$name1;
+ $self->{colHash}->{$name1}=$c2;
+ $self->{colHash}->{$name2}=$c1;
+ $self->rotate() unless $self->{type};
+ my $data1=$self->{data}->[$c1];
+ my $data2=$self->{data}->[$c2];
+ $self->{data}->[$c1]=$data2;
+ $self->{data}->[$c2]=$data1;
+ return 1;
+}
+
+sub moveCol {
+ my ($self, $colID, $colIdx, $newColName) = @_;
+ my $c=$self->checkOldCol($colID);
+ return undef unless defined $c;
+ confess "New column location out of bound!" unless ($colIdx >= 0 && $colIdx < $self->nofCol);
+ return if $c == $colIdx;
+ my $colName = $self->{header}->[$c];
+ my $col = $self->delCol($colID);
+ $self->addCol($col, $colName, $colIdx);
+ $self->rename($colIdx, $newColName) if defined $newColName;
+ return 1;
+}
+
+sub checkOldRow {
+ my ($self, $rowIdx) = @_;
+ my $maxIdx=$self->nofRow()-1;
+ unless (defined $rowIdx) {
+ print STDERR " Invalid row index in call to checkOldRow\n";
+ return undef;
+ }
+ if ($rowIdx<0 || $rowIdx>$maxIdx) {
+ print STDERR "Row index out of range [0..$maxIdx]" ;
+ return undef;
+ }
+ return $rowIdx;
+}
+
+sub checkNewRow {
+ my ($self, $rowIdx) = @_;
+ my $maxIdx=$self->nofRow()-1;
+ unless (defined $rowIdx) {
+ print STDERR "Invalid row index: $rowIdx \n";
+ return undef;
+ }
+ $maxIdx+=1;
+ if ($rowIdx<0 || $rowIdx>$maxIdx) {
+ print STDERR "Row index out of range [0..$maxIdx]" ;
+ return undef;
+ }
+ return $rowIdx;
+}
+
+sub checkOldCol {
+ my ($self, $colID) = @_;
+ my $c=$self->colIndex($colID);
+ if ($c < 0) {
+ print STDERR "Invalid column $colID";
+ return undef;
+ }
+ return $c;
+}
+
+sub checkNewCol {
+ my ($self, $colIdx, $colName) = @_;
+ my $numCol=$self->nofCol();
+ unless (defined $colIdx) {
+ print STDERR "Invalid column index $colIdx";
+ return undef;
+ }
+ if ($colIdx<0 || $colIdx>$numCol) {
+ print STDERR "Column index $colIdx out of range [0..$numCol]";
+ return undef;
+ }
+ if (defined $self->{colHash}->{$colName} ) {
+ print STDERR "Column name $colName already exists" ;
+ return undef;
+ }
+ unless ($colName =~ /\D/) {
+ print STDERR "Invalid column name $colName" ;
+ return undef;
+ }
+ return $colIdx;
+}
+
+sub elm {
+ my ($self, $rowIdx, $colID) = @_;
+ my $c=$self->checkOldCol($colID);
+ return undef unless defined $c;
+ return undef unless defined $self->checkOldRow($rowIdx);
+ return ($self->{type})?
+ $self->{data}->[$c]->[$rowIdx]:
+ $self->{data}->[$rowIdx]->[$c];
+}
+
+sub elmRef {
+ my ($self, $rowIdx, $colID) = @_;
+ my $c=$self->checkOldCol($colID);
+ return undef unless defined $c;
+ return undef unless defined $self->checkOldRow($rowIdx);
+ return ($self->{type})?
+ \$self->{data}->[$c]->[$rowIdx]:
+ \$self->{data}->[$rowIdx]->[$c];
+}
+
+sub setElm {
+ my ($self, $rowIdx, $colID, $val) = @_;
+ $rowIdx = [$rowIdx] if ref($rowIdx) eq '';
+ $colID = [$colID] if ref($colID) eq '';
+ foreach my $col (@$colID) {
+ my $c=$self->checkOldCol($col);
+ return undef unless defined $c;
+ foreach my $row (@$rowIdx) {
+ return undef unless defined $self->checkOldRow($row);
+ if ($self->{type}) {
+ $self->{data}->[$c]->[$row]=$val;
+ } else {
+ $self->{data}->[$row]->[$c]=$val;
+ }
+ }
+ }
+ return 1;
+}
+
+# convert the internal structure of a table between row-based and column-based
+sub rotate {
+ my $self=shift;
+ my $newdata=[];
+ my $data=$self->{data};
+ $self->{type} = ($self->{type})?0:1;
+ if ($self->{type} && scalar @$data == 0) {
+ for (my $i=0; $i < $self->nofCol; $i++) {
+ $newdata->[$i] = [];
+ }
+ } else {
+ for (my $i=$#{$data->[0]}; $i>=0; $i--) {
+ for (my $j=$#{$data}; $j>=0; $j--) {
+ $newdata->[$i][$j]=$data->[$j][$i];
+ }
+ }
+ }
+ $self->{data}=$newdata;
+ return 1;
+}
+
+sub header {
+ my ($self, $header) = @_;
+ unless (defined($header)) {
+ return @{$self->{header}};
+ } else {
+ if (scalar @$header != scalar @{$self->{header}}) {
+ confess "Header array should have size ".(scalar @{$self->{header}});
+ } else {
+ my $colHash = checkHeader($header);
+ $self->{header} = $header;
+ $self->{colHash} = $colHash;
+ }
+ }
+}
+
+sub type {
+ my $self=shift;
+ return $self->{type};
+}
+
+sub data {
+ my $self=shift;
+ return $self->{data};
+}
+
+# $t->sort(colID1, type1, order1, colID2, type2, order2, ... );
+# where
+# colID is a column index (integer) or name (string),
+# type is 0 for numerical and 1 for others
+# order is 0 for ascending and 1 for descending
+# Sorting is done with priority of colname1, colname2, ...
+
+sub sort_v0 {
+ my $self = shift;
+ my ($str, $i) = ("", 0);
+ my @cols = ();
+ while (scalar @_) {
+ my $c = shift;
+ my $col = $self->checkOldCol($c);
+ return undef unless defined $col;
+ push @cols, $col;
+ my $op = '<=>';
+ $op = 'cmp' if shift; # string
+ $str .=(shift)? "(\$b->[$i] $op \$a->[$i]) || " :
+ "(\$a->[$i] $op \$b->[$i]) || " ;
+ $i++;
+ }
+ substr($str, -3) = ""; # removes || from the end of $str
+ $self->rotate() if $self->{type};
+ # construct a pre-ordered array
+ my $fun = sub { my ($cols, $data) = @_;
+ my @ext;
+ @ext = map {$data->[$_]} @$cols;
+ push @ext, $data;
+ return \@ext;
+ };
+ my @preordered = map {&$fun(\@cols, $_)} @{$self->{data}};
+ $self->{data} = [ map {$_->[$i]} eval "sort {$str} \@preordered;" ];
+ return 1;
+}
+
+sub sort {
+ my $self = shift;
+ my @cols = @_;
+ confess "Parameters be in groups of three!\n" if ($#cols % 3 != 2);
+ foreach (0 .. ($#cols/3)) {
+ my $col = $self->checkOldCol($cols[$_*3]);
+ return undef unless defined $col;
+ $cols[$_*3]=$col;
+ }
+ my @subs=();
+ for (my $i=0; $i<=$#cols; $i+=3) {
+ my $mysub;
+ if ($cols[$i+1] == 0) {
+ $mysub = ($cols[$i+2]? sub {defined($_[1])?(defined($_[0])? $_[1] <=> $_[0]:1):(defined($_[0])?-1:0)} : sub {defined($_[1])?(defined($_[0])? $_[0] <=> $_[1]:-1):(defined($_[0])?1:0)});
+ } elsif ($cols[$i+1] == 1) {
+ $mysub = ($cols[$i+2]? sub {defined($_[1])?(defined($_[0])? $_[1] cmp $_[0]:1):(defined($_[0])?-1:0)} : sub {defined($_[1])?(defined($_[0])? $_[0] cmp $_[1]:-1):(defined($_[0])?1:0)});
+ } elsif (ref $cols[$i+1] eq 'CODE') {
+ my $predicate=$cols[$i+1];
+ $mysub = ($cols[$i+2]? sub {defined($_[1])?(defined($_[0])? $predicate->($_[1],$_[0]) : 1): (defined($_[0])?-1:0)} :
+ sub {defined($_[1])?(defined($_[0])? $predicate->($_[0],$_[1]) : -1): (defined($_[0])?1:0)} );
+ } else {
+ confess "Sort method should be 0 (numerical), 1 (other type), or a subroutine reference!\n";
+ }
+ push @subs, $mysub;
+ }
+ my $func = sub {
+ my $res = 0;
+ foreach (0 .. ($#cols/3)) {
+ $res ||= $subs[$_]->($a->[$cols[$_*3]], $b->[$cols[$_*3]]);
+ return $res unless $res==0;
+ }
+ return $res;
+ };
+ $self->rotate() if $self->{type};
+ $self->{data} = [sort $func @{$self->{data}}];
+ return 1;
+}
+
+# return rows as sub table in which
+# a pattern $pattern is matched
+sub match_pattern {
+ my ($self, $pattern, $countOnly) = @_;
+ my @data=();
+ $countOnly=0 unless defined($countOnly);
+ my $cnt=0;
+ $self->rotate() if $self->{type};
+ @Data::Table::OK= eval "map { $pattern?1:0; } \@{\$self->{data}};";
+ my @ok = @Data::Table::OK;
+ $self->{OK} = \@ok;
+ for (my $i=0; $i<$self->nofRow(); $i++) {
+ if ($self->{OK}->[$i]) {
+ push @data, $self->{data}->[$i] unless $countOnly;
+ $cnt++;
+ $self->{OK}->[$i] = 1;
+ $Data::Table::OK[$i] = 1;
+ } else {
+ # in case sometimes eval results is '' instead of 0
+ $self->{OK}->[$i] = 0;
+ $Data::Table::OK[$i] = 0;
+ }
+ }
+ $self->{MATCH} = [];
+ map { push @{$self->{MATCH}}, $_ if $self->{OK}->[$_] } 0 .. $#ok;
+ return $cnt if $countOnly;
+ my @header=@{$self->{header}};
+ return new Data::Table(\@data, \@header, 0);
+}
+
+# return rows as sub table in which
+# a pattern $pattern is matched
+# each row is passed to the patern as a hash, where column names are keys
+sub match_pattern_hash {
+ my ($self, $pattern, $countOnly) = @_;
+ my @data=();
+ $countOnly=0 unless defined($countOnly);
+ my $cnt=0;
+ $self->rotate() if $self->{type};
+ @Data::Table::OK = ();
+ for (my $i=0; $i<$self->nofRow(); $i++) {
+ local %_ = %{$self->rowHashRef($i)};
+ $Data::Table::OK[$i] = eval "$pattern?1:0";
+ }
+ #@Data::Table::OK= eval "map { $pattern?1:0; } \@{\$self->{data}};";
+ my @ok = @Data::Table::OK;
+ $self->{OK} = \@ok;
+ for (my $i=0; $i<$self->nofRow(); $i++) {
+ if ($self->{OK}->[$i]) {
+ push @data, $self->{data}->[$i] unless $countOnly;
+ $cnt++;
+ $self->{OK}->[$i] = 1;
+ $Data::Table::OK[$i] = 1;
+ } else {
+ # in case sometimes eval results is '' instead of 0
+ $self->{OK}->[$i] = 0;
+ $Data::Table::OK[$i] = 0;
+ }
+ }
+ $self->{MATCH} = [];
+ map { push @{$self->{MATCH}}, $_ if $self->{OK}->[$_] } 0 .. $#ok;
+ return $cnt if $countOnly;
+ my @header=@{$self->{header}};
+ return new Data::Table(\@data, \@header, 0);
+}
+
+# return rows as sub table in which
+# a string elm in an array @$s is matched
+sub match_string {
+ my ($self, $s, $caseIgn, $countOnly) = @_;
+ confess unless defined($s);
+ $countOnly=0 unless defined($countOnly);
+ my @data=();
+ my $r;
+ $self->rotate() if $self->{type};
+ @Data::Table::OK=();
+ $self->{OK} = [];
+ $caseIgn=0 unless defined($caseIgn);
+
+ ### comment out next line if your perl version < 5.005 ###
+ $r = ($caseIgn)?qr/$s/i : qr/$s/;
+ my $cnt=0;
+
+ foreach my $row_ref (@{$self->data}) {
+ push @Data::Table::OK, 0;
+ push @{$self->{OK}}, 0;
+ foreach my $elm (@$row_ref) {
+ next unless defined($elm);
+
+ ### comment out the next line if your perl version < 5.005
+ if ($elm =~ /$r/) {
+ ### uncomment the next line if your perl version < 5.005
+ # if ($elm =~ /$s/ || ($elm=~ /$s/i && $caseIgn)) {
+
+ push @data, $row_ref unless $countOnly;
+ $Data::Table::OK[$#Data::Table::OK]=1;
+ $self->{OK}->[$#{$self->{OK}}]=1;
+ $cnt++;
+ last;
+ }
+ }
+ }
+ $self->{MATCH} = [];
+ map { push @{$self->{MATCH}}, $_ if $self->{OK}->[$_] } 0 .. $#{$self->{OK}};
+ return $cnt if $countOnly;
+ my @header=@{$self->{header}};
+ return new Data::Table(\@data, \@header, 0);
+}
+
+sub rowMask {
+ my ($self, $OK, $c) = @_;
+ confess unless defined($OK);
+ $c = 0 unless defined ($c);
+ my @data=();
+ $self->rotate() if $self->{type};
+ my $data0=$self->data;
+ for (my $i=0; $i<$self->nofRow(); $i++) {
+ if ($c) {
+ push @data, $data0->[$i] unless $OK->[$i];
+ } else {
+ push @data, $data0->[$i] if $OK->[$i];
+ }
+ }
+ my @header=@{$self->{header}};
+ return new Data::Table(\@data, \@header, 0);
+}
+
+sub rowMerge {
+ my ($self, $tbl, $arg_ref) = @_;
+ my %arg = defined $arg_ref ? %$arg_ref : ();
+ $arg{byName} =0 unless exists $arg{byName};
+ $arg{addNewCol} = 0 unless exists $arg{addNewCol};
+ if ($self->isEmpty && !$tbl->isEmpty) {
+ my @header = $tbl->header;
+ my $i = 0;
+ foreach my $s (@header) {
+ push @{$self->{header}}, $s;
+ $self->{colHash}->{$s} = $i++;
+ }
+ }
+ if ($arg{byName} == 0 && $arg{addNewCol} == 0) {
+ confess "Tables must have the same number of columns" unless ($self->nofCol()==$tbl->nofCol());
+ } else {
+ if ($arg{addNewCol}) {
+ unless ($arg{byName}) { # add extra column by index
+ if ($self->nofCol < $tbl->nofCol) {
+ my @header = $tbl->header;
+ my $nCols = $self->nofCol();
+ my $nRows = $self->nofRow();
+ for (my $i = $nCols; $i<@header; $i++) {
+ my @one = (undef) x $nRows;
+ $self->addCol(\@one, $header[$i]);
+ }
+ } elsif ($self->nofCol > $tbl->nofCol) {
+ my @header = $self->header;
+ my %h = ();
+ my @header2 = $tbl->header;
+ map {$h{$_} = 1} @header2;
+ my $nCols = $tbl->nofCol();
+ my $nRows = $tbl->nofRow();
+ for (my $i = $nCols; $i<$self->nofCol; $i++) {
+ my @one = (undef) x $nRows;
+ # make sure new col name is unique
+ my $s = $header[$i];
+ my $cnt = 2;
+ while (exists $h{$s}) {
+ $s = $header[$i]."_".$cnt ++;
+ }
+ $tbl->addCol(\@one, $s);
+ $h{$s} = 1;
+ }
+ }
+ } else {
+ my @header = $tbl->header;
+ my $nRows = $self->nofRow();
+ foreach my $col (@header) {
+ if ($self->colIndex($col) < 0) {
+ my @one = (undef) x $nRows;
+ $self->addCol(\@one, $col);
+ }
+ }
+ }
+ }
+ }
+ $self->rotate() if $self->{type};
+ $tbl->rotate() if $tbl->{type};
+ my $data=$self->{data};
+ if ($arg{byName} == 0) {
+ push @$data, @{$tbl->{data}};
+ } else {
+ my @header = $self->header;
+ my $nCols = scalar @header;
+ my @colIndex = map { $tbl->colIndex($_) } @header;
+ foreach my $rowRef (@{$tbl->{data}}) {
+ my @one = ();
+ for (my $j=0; $j< $nCols; $j++) {
+ $one[$j] = $colIndex[$j]>=0 ? $rowRef->[$colIndex[$j]]:undef;
+ }
+ push @$data, \@one;
+ }
+ }
+ return 1;
+}
+
+sub colMerge {
+ my ($self, $tbl, $arg_ref) = @_;
+ my %arg = defined $arg_ref ? %$arg_ref : ();
+ $arg{renameCol} =0 unless exists $arg{renameCol};
+ confess "Tables must have the same number of rows" unless ($self->isEmpty || $self->nofRow()==$tbl->nofRow());
+ my $col;
+ my %h = ();
+ map {$h{$_} = 1} @{$self->{header}};
+ my @header2 = ();
+ foreach $col ($tbl->header) {
+ my $s = $col;
+ if (exists $h{$s}) {
+ confess "Duplicate column $col in two tables" unless $arg{renameCol};
+ my $cnt = 2;
+ while (exists $h{$s}) {
+ $s = $col ."_". $cnt++;
+ }
+ }
+ $h{$s} = 1;
+ push @header2, $s;
+ }
+ $self->rotate() unless $self->{type};
+ $tbl->rotate() unless $tbl->{type};
+ my $i = $self->nofCol();
+ for my $s (@header2) {
+ push @{$self->{header}}, $s;
+ $self->{colHash}->{$s} = $i++;
+ }
+ my $data=$self->{data};
+ for ($i=0; $i<$tbl->nofCol(); $i++) {
+ push @$data, $tbl->{data}->[$i];
+ }
+ return 1;
+}
+
+sub subTable {
+ my ($self, $rowIdcsRef, $colIDsRef, $arg_ref) = @_;
+ my @newdata=();
+ my @newheader=();
+ # to avoid the side effect of modifying $colIDsRef, 4/30/2012
+ my $useRowMask = 0;
+ $useRowMask = $arg_ref->{useRowMask} if defined $arg_ref->{useRowMask};
+ my @rowIdcs = ();
+ @rowIdcs = defined $rowIdcsRef ? @$rowIdcsRef : 0..($self->nofRow()-1) unless $useRowMask;
+ my @colIDs = defined $colIDsRef ? @$colIDsRef : 0..($self->nofCol()-1);
+ ##$rowIdcsRef = [0..($self->nofRow()-1)] unless defined $rowIdcsRef;
+ #$colIDsRef = [0..($self->nofCol()-1)] unless defined $colIDsRef;
+ for (my $i = 0; $i < scalar @colIDs; $i++) {
+ $colIDs[$i]=$self->checkOldCol($colIDs[$i]);
+ #return undef unless defined $colIDsRef;
+ push @newheader, $self->{header}->[$colIDs[$i]];
+ }
+ if ($useRowMask) {
+ my @OK = @$rowIdcsRef;
+ my $n = $self->nofRow;
+ for (my $i = 0; $i < $n; $i++) {
+ push @rowIdcs, $i if $OK[$i];
+ }
+ }
+ if ($self->{type}) {
+ for (my $i = 0; $i < scalar @colIDs; $i++) {
+ my @one=();
+ for (my $j = 0; $j < scalar @rowIdcs; $j++) {
+ return undef unless defined $self->checkOldRow($rowIdcs[$j]);
+ push @one, $self->{data}->[$colIDs[$i]]->[$rowIdcs[$j]];
+ }
+ push @newdata, \@one;
+ }
+ } else {
+ for (my $i = 0; $i < scalar @rowIdcs; $i++) {
+ return undef unless defined $self->checkOldRow($rowIdcs[$i]);
+ my @one=();
+ for (my $j = 0; $j < scalar @colIDs; $j++) {
+ push @one, $self->{data}->[$rowIdcs[$i]]->[$colIDs[$j]];
+ }
+ push @newdata, \@one;
+ }
+ }
+ return new Data::Table(\@newdata, \@newheader, $self->{type});
+}
+
+sub reorder {
+ my ($self, $colIDsRef, $arg_ref) = @_;
+ return unless defined $colIDsRef;
+ $arg_ref = {keepRest => 1} unless defined $arg_ref;
+ my @newdata=();
+ my @newheader=();
+ my @colIDs = ();
+ my %inNew = ();
+ for (my $i = 0; $i < scalar @$colIDsRef; $i++) {
+ my $idx = $self->checkOldCol($colIDsRef->[$i]);
+ confess "Invalide column $colIDsRef->[$i]" unless defined $idx;
+ $colIDs[$i] = $idx;
+ $inNew{$idx} = 1;
+ #return undef unless defined $colIDsRef;
+ push @newheader, $self->{header}->[$idx];
+ }
+ if ($arg_ref->{keepRest}) {
+ for (my $i = 0; $i<$self->nofCol; $i++) {
+ unless (exists $inNew{$i}) {
+ push @colIDs, $i;
+ push @newheader, $self->{header}->[$i];
+ }
+ }
+ }
+
+ if ($self->{type}) {
+ for (my $i = 0; $i < scalar @colIDs; $i++) {
+ push @newdata, $self->{data}->[$colIDs[$i]];
+ }
+ } else {
+ my $n = $self->nofRow;
+ for (my $i = 0; $i < $n; $i++) {
+ my @one=();
+ for (my $j = 0; $j < scalar @colIDs; $j++) {
+ push @one, $self->{data}->[$i]->[$colIDs[$j]];
+ }
+ push @newdata, \@one;
+ }
+ }
+ $self->{header} = \@newheader;
+ $self->{colHash} = ();
+ for (my $i = 0; $i < scalar @colIDs; $i++) {
+ $self->{colHash}->{$newheader[$i]} = $i;
+ }
+ $self->{data} = \@newdata;
+}
+
+sub clone {
+ my $self = shift;
+ my $data = $self->{data};
+ my @newheader = @{$self->{header}};
+ my @newdata = ();
+ for (my $i = 0; $i < scalar @{$data}; $i++) {
+ my @one=();
+ for (my $j = 0; $j < scalar @{$data->[$i]}; $j++) {
+ push @one, $data->[$i]->[$j];
+ }
+ push @newdata, \@one;
+ }
+ return new Data::Table(\@newdata, \@newheader, $self->{type});
+}
+
+sub fromCSVi {
+ my $self = shift;
+ return fromCSV(@_);
+}
+
+sub getOneLine {
+ my ($fh, $linebreak, $qualifier) = @_;
+ my $s = '';
+ $qualifier = '' unless defined $qualifier;
+ local($/) = $linebreak;
+ return <$fh> unless $qualifier;
+ while (my $s2 = <$fh>) {
+ $s .= $s2;
+ my @S = ($s =~ /$qualifier/g);
+ return $s if (scalar @S % 2 == 0);
+ }
+ return $s;
+}
+
+sub fromCSV {
+ my ($name_or_handler, $includeHeader, $header, $arg_ref) = @_;
+ $includeHeader = 1 unless defined($includeHeader);
+ my ($OS, $delimiter, $qualifier, $skip_lines, $skip_pattern, $encoding) = ($Data::Table::DEFAULTS{OS}, $Data::Table::DEFAULTS{CSV_DELIMITER}, $Data::Table::DEFAULTS{CSV_QUALIFIER}, 0, undef, $Data::Table::DEFAULTS{ENCODING});
+ $OS = $arg_ref->{'OS'} if (defined($arg_ref) && defined($arg_ref->{'OS'}));
+ # OS: 0 for UNIX (\n as linebreak), 1 for Windows (\r\n as linebreak)
+ ### 2 for MAC (\r as linebreak)
+ if (defined($arg_ref)) {
+ $delimiter = $arg_ref->{'delimiter'} if defined($arg_ref->{'delimiter'});
+ $qualifier = $arg_ref->{'qualifier'} if defined($arg_ref->{'qualifier'});
+ $skip_lines = $arg_ref->{'skip_lines'} if (defined($arg_ref->{'skip_lines'}) && $arg_ref->{'skip_lines'}>0);
+ $skip_pattern = $arg_ref->{'skip_pattern'} if defined($arg_ref->{'skip_pattern'});
+ $encoding = $arg_ref->{'encoding'} if defined($arg_ref->{'encoding'});
+ }
+ my @header;
+ my $givenHeader = 0;
+ if (defined($header) && ref($header) eq 'ARRAY') {
+ $givenHeader = 1;
+ @header= @$header;
+ }
+ my $SRC=openFileWithEncoding($name_or_handler, $encoding);
+ my @data = ();
+ my $oldRowDelimiter=$/;
+ my $newRowDelimiter=($OS==2)?"\r":(($OS==1)?"\r\n":"\n");
+ my $n_endl = length($newRowDelimiter);
+ $/=$newRowDelimiter;
+ my $s;
+ for (my $i=0; $i<$skip_lines; $i++) {
+ #$s=<$SRC>;
+ $s = getOneLine($SRC, $newRowDelimiter, $qualifier);
+ }
+ #$s=<$SRC>;
+ $s = getOneLine($SRC, $newRowDelimiter, $qualifier);
+ if (defined($skip_pattern)) { while (defined($s) && $s =~ /$skip_pattern/) { $s = getOneLine($SRC, $newRowDelimiter, $qualifier); }}
+ #{ $s = <$SRC> }; }
+ if (substr($s, -$n_endl, $n_endl) eq $newRowDelimiter) { for (1..$n_endl) { chop $s }}
+ # $_=~ s/$newRowDelimiter$//;
+ unless ($s) {
+ #confess "Empty data file" unless $givenHeader;
+ return undef unless $givenHeader;
+ $/=$oldRowDelimiter;
+ return new Data::Table(\@data, \@header, 0);
+ }
+ my $one;
+ if ($s =~ /$delimiter$/) { # if the line ends by ',', the size of @one will be incorrect
+ # due to the tailing of split function in perl
+ $s .= ' '; # e.g., split $s="a," will only return a list of size 1.
+ $one = parseCSV($s, undef, {delimiter=>$delimiter, qualifier=>$qualifier});
+ $one->[$#{@$one}]=undef;
+ } else {
+ $one = parseCSV($s, undef, {delimiter=>$delimiter, qualifier=>$qualifier});
+ }
+ #print join("|", @$one), scalar @$one, "\n";
+ my $size = scalar @$one;
+ unless ($givenHeader) {
+ if ($includeHeader) {
+ @header = @$one;
+ } else {
+ @header = map {"col$_"} (1..$size); # name each column as col1, col2, .. etc
+ }
+ }
+ push @data, $one unless ($includeHeader);
+
+ #while($s = <$SRC>) {
+ while($s = getOneLine($SRC, $newRowDelimiter, $qualifier)) {
+ next if (defined($skip_pattern) && $s =~ /$skip_pattern/);
+ if (substr($s, -$n_endl, $n_endl) eq $newRowDelimiter) { for (1..$n_endl) { chop $s }}
+ # $_=~ s/$newDelimiter$//;
+ my $one = parseCSV($s, $size, {delimiter=>$delimiter, qualifier=>$qualifier});
+ confess "Inconsistent column number at data entry: ".($#data+1) unless ($size==scalar @$one);
+ push @data, $one;
+ }
+ close($SRC);
+ $/=$oldRowDelimiter;
+ return new Data::Table(\@data, \@header, 0);
+}
+
+# Idea: use \ as the escape char to encode a CSV string,
+# replace \ by \\ and comma inside a field by \c.
+# A comma inside a field must have odd number of " in front of it,
+# therefore it can be distinguished from comma used as the deliminator.
+# After escape, and split by comma, we unescape each field string.
+#
+# This parser will never be crashed by any illegal CSV format,
+# it always return an array!
+sub parseCSV {
+ my ($s, $size, $arg_ref)=@_;
+ $size = 0 unless defined $size;
+ my ($delimiter, $qualifier) = ($Data::Table::DEFAULTS{CSV_DELIMITER}, $Data::Table::DEFAULTS{CSV_QUALIFIER});
+ $delimiter = $arg_ref->{'delimiter'} if (defined($arg_ref) && defined($arg_ref->{'delimiter'}));
+ $qualifier = $arg_ref->{'qualifier'} if (defined($arg_ref) && defined($arg_ref->{'qualifier'}));
+ my $delimiter2 = $delimiter; $delimiter2 = substr($delimiter, 1, 1) if length($delimiter)>1;
+ my $qualifier2 = $qualifier; $qualifier2 = substr($qualifier, 1, 1) if length($qualifier)>1;
+ # $s =~ s/\n$//; # chop" # assume extra characters has been cleaned before
+ if (-1==index $s, $qualifier) {
+ if ($size == 0) {
+ my $s2 = $s;
+ $s2 =~ s/$delimiter//g;
+ $size = length($s)-length($s2)+1;
+ }
+ return [split /$delimiter/, $s , $size];
+ }
+ $s =~ s/\\/\\\\/g; # escape \ => \\
+ my $n = length($s);
+ my ($q, $i)=(0, 0);
+ while ($i < $n) {
+ my $ch=substr($s, $i, 1);
+ $i++;
+ if ($ch eq $delimiter2 && ($q%2)) {
+ substr($s, $i-1, 1)='\\c'; # escape , => \c if it's not a deliminator
+ $i++;
+ $n++;
+ } elsif ($ch eq $qualifier2) {
+ $q++;
+ }
+ }
+ $s =~ s/(^$qualifier)|($qualifier\s*$)//g; # get rid of boundary ", then restore "" => "
+ $s =~ s/$qualifier\s*$delimiter/$delimiter2/g;
+ $s =~ s/$delimiter\s*$qualifier/$delimiter2/g;
+ $s =~ s/$qualifier$qualifier/$qualifier2/g;
+ if ($size == 0) {
+ my $s2 = $s;
+ $s2 =~ s/$delimiter//g;
+ $size = length($s)-length($s2)+1;
+ }
+ my @parts=split(/$delimiter/, $s, $size);
+ @parts = map {$_ =~ s/(\\c|\\\\)/$1 eq '\c'?$delimiter2:'\\'/eg; $_ } @parts;
+# my @parts2=();
+# foreach $s2 (@parts) {
+# $s2 =~ s/\\c/,/g; # restore \c => ,
+# $s2 =~ s/\\\\/\\/g; # restore \\ => \
+# push @parts2, $s2;
+# }
+ return \@parts;
+}
+
+sub transformElement {
+ my $one = shift;
+ for (my $i=0; $i < scalar @$one; $i++) {
+ next unless defined($one->[$i]);
+ if ($one->[$i] eq "\\N") {
+ $one->[$i]=undef;
+ } else {
+ $one->[$i] =~ s/\\([0ntrb'"\\])/$Data::Table::TSV_ESC{$1}/g;
+ }
+ }
+ return $one;
+}
+
+sub fromTSVi {
+ my $self = shift;
+ return fromTSV(@_);
+}
+
+sub fromTSV {
+ my ($name_or_handler, $includeHeader, $header, $arg_ref) = @_;
+ my ($OS, $skip_lines, $skip_pattern, $transform_element, $encoding) = ($Data::Table::DEFAULTS{OS}, 0, undef, 1, $Data::Table::DEFAULTS{ENCODING});
+ $OS = $arg_ref->{'OS'} if (defined($arg_ref) && defined($arg_ref->{'OS'}));
+ # OS: 0 for UNIX (\n as linebreak), 1 for Windows (\r\n as linebreak)
+ ### 2 for MAC (\r as linebreak)
+ $skip_lines = $arg_ref->{'skip_lines'} if (defined($arg_ref) && defined($arg_ref->{'skip_lines'}) && $arg_ref->{'skip_lines'}>0);
+ $skip_pattern = $arg_ref->{'skip_pattern'} if defined($arg_ref->{'skip_pattern'});
+ $transform_element = $arg_ref->{'transform_element'} if (defined($arg_ref->{'transform_element'}));
+ $encoding = $arg_ref->{'encoding'} if (defined($arg_ref->{'encoding'}));
+ #my %ESC = ( '0'=>"\0", 'n'=>"\n", 't'=>"\t", 'r'=>"\r", 'b'=>"\b",
+ # "'"=>"'", '"'=>"\"", '\\'=>"\\" );
+ ## what about \f? MySQL treats \f as f.
+
+ $includeHeader = 1 unless defined($includeHeader);
+ $OS=0 unless defined($OS);
+
+ my @header;
+ my $givenHeader = 0;
+ if (defined($header) && ref($header) eq 'ARRAY') {
+ $givenHeader = 1;
+ @header= @$header;
+ }
+ my $SRC=openFileWithEncoding($name_or_handler, $encoding);
+ my @data = ();
+ my $oldRowDelimiter=$/;
+ my $newRowDelimiter=($OS==2)?"\r":(($OS==1)?"\r\n":"\n");
+ my $n_endl = length($newRowDelimiter);
+ $/=$newRowDelimiter;
+ my $s;
+ for (my $i=0; $i<$skip_lines; $i++) {
+ $s=<$SRC>;
+ }
+ $s=<$SRC>;
+ if (defined($skip_pattern)) { while (defined($s) && $s =~ /$skip_pattern/) { $s = <$SRC> }; }
+ if (substr($s, -$n_endl, $n_endl) eq $newRowDelimiter) { for (1..$n_endl) { chop $s }}
+ # $_=~ s/$newRowDelimiter$//;
+ unless ($s) {
+ confess "Empty data file" unless $givenHeader;
+ $/=$oldRowDelimiter;
+ return new Data::Table(\@data, \@header, 0);
+ }
+ #chop;
+ my $one;
+ if ($s =~ /\t$/) { # if the line ends by ',', the size of @$one will be incorrect
+ # due to the tailing of split function in perl
+ $s .= ' '; # e.g., split $s="a," will only return a list of size 1.
+ @$one = split(/\t/, $s);
+ $one->[$#{@$one}]='';
+ } else {
+ @$one = split(/\t/, $s);
+ }
+ # print join("|", @$one), scalar @$one, "\n";
+ my $size = scalar @$one;
+ unless ($givenHeader) {
+ if ($includeHeader) {
+ if ($transform_element) {
+ @header = map { $_ =~ s/\\([0ntrb'"\\])/$Data::Table::TSV_ESC{$1}/g; $_ } @$one;
+ } else {
+ @header = @$one;
+ }
+ } else {
+ @header = map {"col$_"} (1..$size); # name each column as col1, col2, .. etc
+ }
+ }
+ unless ($includeHeader) {
+ transformElement($one) if $transform_element;
+ push @data, $one;
+ }
+ while($s = <$SRC>) {
+ #chop;
+ # $_=~ s/$newRowDelimiter$//;
+ next if (defined($skip_pattern) && $s =~ /$skip_pattern/);
+ if (substr($s, -$n_endl, $n_endl) eq $newRowDelimiter) { for (1..$n_endl) { chop $s }}
+ my @one = split(/\t/, $s, $size);
+ transformElement(\@one) if $transform_element;
+ #for (my $i=0; $i < $size; $i++) {
+ # next unless defined($one[$i]);
+ # if ($one[$i] eq "\\N") {
+ # $one[$i]=undef;
+ # } else {
+ # $one[$i] =~ s/\\([0ntrb'"\\])/$Data::Table::TSV_ESC{$1}/g;
+ # }
+ #}
+ confess "Inconsistent column number at data entry: ".($#data+1) unless ($size==scalar @one);
+ push @data, \@one;
+ }
+ close($SRC);
+ $/=$oldRowDelimiter;
+ return new Data::Table(\@data, \@header, 0);
+}
+
+sub fromSQLi {
+ my $self = shift;
+ return fromSQL(@_);
+}
+
+sub fromSQL {
+ my ($dbh, $sql, $vars) = @_;
+ my ($sth, $header, $t);
+ if (ref $sql eq 'DBI::st') {
+ $sth = $sql;
+ } else {
+ $sth = $dbh->prepare($sql) or confess "Preparing: , ".$dbh->errstr;
+ }
+ my @vars=() unless defined $vars;
+ $sth->execute(@$vars) or confess "Executing: ".$dbh->errstr;
+# $Data::Table::ID = undef;
+# $Data::Table::ID = $sth->{'mysql_insertid'};
+ if ($sth->{NUM_OF_FIELDS}) {
+ $header=$sth->{'NAME'};
+ $t = new Data::Table($sth->fetchall_arrayref(), $header, 0);
+ } else {
+ $t = undef;
+ }
+ $sth->finish;
+ return $t;
+}
+
+sub join {
+ my ($self, $tbl, $type, $cols1, $cols2, $arg_ref) = @_;
+ my $n1 = scalar @$cols1;
+ my %arg= ( renameCol => 0, matchNULL => 0, NULLasEmpty => 0);
+ $arg{renameCol} = $arg_ref->{renameCol} if exists $arg_ref->{renameCol};
+ $arg{matchNULL} = $arg_ref->{matchNULL} if exists $arg_ref->{matchNULL};
+ $arg{NULLasEmpty} = $arg_ref->{NULLasEmpty} if exists $arg_ref->{NULLasEmpty};
+ #%arg = %$arg_ref if defined $arg_ref;
+ # default cols2 to cols1 if not specified
+ if (!defined($cols2) && $n1>0) {
+ $cols2 = [];
+ foreach my $c (@$cols1) {
+ push @$cols2, $c;
+ }
+ }
+ my $n2 = scalar @$cols2;
+ confess "The number of join columns must be the same: $n1 != $n2" unless $n1==$n2;
+ confess "At least one join column must be specified" unless $n1;
+ my ($i, $j, $k);
+ my @cols3 = ();
+ for ($i = 0; $i < $n1; $i++) {
+ $cols1->[$i]=$self->checkOldCol($cols1->[$i]);
+ confess "Unknown column ". $cols1->[$i] unless defined($cols1->[$i]);
+ $cols2->[$i]=$tbl->checkOldCol($cols2->[$i]);
+ confess "Unknown column ". $cols2->[$i] unless defined($cols2->[$i]);
+ $cols3[$cols2->[$i]]=1;
+ }
+ my @cols4 = (); # the list of remaining columns
+ my @header2 = ();
+ for ($i = 0; $i < $tbl->nofCol; $i++) {
+ unless (defined($cols3[$i])) {
+ push @cols4, $i;
+ push @header2, $tbl->{header}->[$i];
+ }
+ }
+
+ $self->rotate() if $self->{type};
+ $tbl->rotate() if $tbl->{type};
+ my $data1 = $self->{data};
+ my $data2 = $tbl->{data};
+ my %H=();
+ my $key;
+ my @subRow;
+ for ($i = 0; $i < $self->nofRow; $i++) {
+ @subRow = @{$data1->[$i]}[@$cols1];
+ my @S = map {tsvEscape($_)} @subRow;
+ map { $_ = '' if $_ eq '\\N' } @S if $arg{NULLasEmpty};
+ $key = join("\t", @S);
+ unless (defined($H{$key})) {
+ $H{$key} = [[$i], []];
+ } else {
+ push @{$H{$key}->[0]}, $i;
+ }
+ }
+ for ($i = 0; $i < $tbl->nofRow; $i++) {
+ @subRow = @{$data2->[$i]}[@$cols2];
+ # we intentionally make the second table undef keys to be '\\N\\N',
+ # so that they are different from the first table undef keys
+ # avoid NULL == NULL in the join
+ my @S = map {tsvEscape($_)} @subRow;
+ map { $_ = ($arg{NULLasEmpty})? '':($arg{matchNULL} ? $_ : '\\N\\N') if $_ eq '\\N' } @S;
+ #if ($j>= @S) {
+ $key = join("\t", @S);
+ #} else {
+ # $key = $arg{matchNULL} ? '\\N' : '\\N\\N';
+ #}
+ unless (defined($H{$key})) {
+ $H{$key} = [[], [$i]];
+ } else {
+ push @{$H{$key}->[1]}, $i;
+ }
+ }
+# $type
+# 0: inner join
+# 1: left outer join
+# 2: right outer join
+# 3: full outer join
+ my @ones = ();
+ my @null1 = ();
+ my @null2 = ();
+ my @null3 = ();
+ $null1[$self->nofCol-1]=undef;
+ $null3[$self->nofCol-1]=undef;
+ if ($#cols4>=0) { $null2[$#cols4]=undef; }
+ foreach $key (keys %H) {
+ my ($rows1, $rows2) = @{$H{$key}};
+ my $nr1 = scalar @$rows1;
+ my $nr2 = scalar @$rows2;
+ next if ($nr1 == 0 && ($type == 0 || $type == 1));
+ next if ($nr2 == 0 && ($type == 0 || $type == 2));
+ if ($nr2 == 0 && ($type == 1 || $type == 3)) {
+ for ($i = 0; $i < $nr1; $i++) {
+ push @ones, [$self->row($rows1->[$i]), @null2];
+ }
+ next;
+ }
+ if ($nr1 == 0 && ($type == 2 || $type == 3)) {
+ for ($j = 0; $j < $nr2; $j++) {
+ my @row2 = $tbl->row($rows2->[$j]);
+ for ($k = 0; $k< scalar @$cols1; $k++) {
+ $null3[$cols1->[$k]] = $row2[$cols2->[$k]];
+ }
+ if ($#cols4>=0) {
+ push @ones, [@null3, @row2[@cols4]];
+ } else {
+ push @ones, [@null3];
+ }
+ }
+ next;
+ }
+ for ($i = 0; $i < $nr1; $i++) {
+ for ($j = 0; $j < $nr2; $j++) {
+ my @row2 = $tbl->row($rows2->[$j]);
+ push @ones, [$self->row($rows1->[$i]), @row2[@cols4]];
+ }
+ }
+ }
+ if ($arg{renameCol}) {
+ my %h = ();
+ map {$h{$_} = 1} @{$self->{header}};
+ for (my $i=0; $i<@header2; $i++) {
+ my $s = $header2[$i];
+ my $cnt = 2;
+ while (exists $h{$s}) {
+ $s = $header2[$i] ."_". $cnt++;
+ }
+ $header2[$i] = $s;
+ $h{$s} = 1;
+ }
+ }
+ my $header = [@{$self->{header}}, @header2];
+ return new Data::Table(\@ones, $header, 0);
+}
+
+sub melt {
+ my ($self, $keyCols, $variableCols, $arg_ref) = @_;
+ confess "key columns have to be specified!" unless defined($keyCols) && ref($keyCols) eq "ARRAY";
+ my $variableColName = 'variable';
+ my $valueColName = 'value';
+ my $skip_NULL = 1;
+ my $skip_empty = 0;
+ $variableColName = $arg_ref->{'variableColName'} if (defined($arg_ref) && defined($arg_ref->{'variableColName'}));
+ $valueColName = $arg_ref->{'valueColName'} if (defined($arg_ref) && defined($arg_ref->{'valueColName'}));
+ $skip_NULL = $arg_ref->{'skip_NULL'} if (defined($arg_ref) && defined($arg_ref->{'skip_NULL'}));
+ $skip_empty= $arg_ref->{'skip_empty'} if (defined($arg_ref) && defined($arg_ref->{'skip_empty'}));
+ my @X = ();
+ my %X = ();
+ foreach my $x (@$keyCols) {
+ my $x_idx = $self->checkOldCol($x);
+ confess "Unknown column ". $x unless defined($x_idx);
+ push @X, $x_idx;
+ $X{$x_idx} = 1;
+ }
+ my @Y = ();
+ my %Y = ();
+ unless (defined($variableCols)) {
+ $variableCols = [];
+ foreach my $x (0 .. $self->nofCol-1) {
+ next if $X{$x};
+ push @$variableCols, $x;
+ }
+ }
+ unless (scalar @$variableCols) {
+ confess "Variable columns have to be specified!";
+ }
+ foreach my $y (@$variableCols) {
+ my $y_idx = $self->checkOldCol($y);
+ confess "Unknown column ". $y unless defined($y_idx);
+ push @Y, $y_idx;
+ $Y{$y_idx} = 1;
+ }
+
+ my @newHeader = ();
+ my @header = $self->header;
+ for (my $i=0; $i<= $#X; $i++) {
+ push @newHeader, $header[$X[$i]];
+ }
+ push @newHeader, $variableColName;
+ push @newHeader, $valueColName;
+ my @newRows = ();
+ for (my $i=0; $i<$self->nofRow; $i++) {
+ my $row = $self->rowRef($i);
+ my @key = @$row[@X];
+ foreach my $y (@Y) {
+ next if (!defined($row->[$y]) && $skip_NULL);
+ next if ($row->[$y] eq '' && $skip_empty);
+ my @one = @key;
+ push @one, $header[$y], $row->[$y];
+ push @newRows, \@one;
+ }
+ }
+ return new Data::Table(\@newRows, \@newHeader, 0);
+}
+
+sub cast {
+ my ($self, $colsToGroupBy, $colToSplit, $colToSplitIsStringOrNumeric, $colToCalculate, $funToApply) = @_;
+ #$colToSplit = 'variable' unless defined $colToSplit;
+ #$colToCalculate = 'value' unless defined $colToCalculate;
+ $colsToGroupBy = [] unless defined $colsToGroupBy;
+ my $tmpColName = '_calcColumn';
+ my $cnt = 2;
+ my $s = $tmpColName;
+ while ($self->hasCol($s)) {
+ $s = $tmpColName."_".$cnt++;
+ }
+ $tmpColName = $s;
+ my %grpBy = ();
+ map {$grpBy{$_} = 1} @$colsToGroupBy;
+ my @grpBy = @$colsToGroupBy;
+ confess "colToSplit cannot be contained in the list of colsToGroupBy!" if defined $colToSplit and $grpBy{$colToSplit};
+ push @grpBy, $colToSplit if defined $colToSplit;
+ my $t = $self->group(\@grpBy, [$colToCalculate], [$funToApply], [$tmpColName], 0);
+ $t = $t->pivot($colToSplit, $colToSplitIsStringOrNumeric, $tmpColName, $colsToGroupBy);
+ return $t;
+}
+
+sub each_group {
+ my ($self, $colsToGroupBy, $funToApply) = @_;
+ $colsToGroupBy = [] unless defined $colsToGroupBy;
+ confess "colsToGroupBy has to be specified!" unless defined($colsToGroupBy) && ref($colsToGroupBy) eq "ARRAY";
+ confess "funToApply has to be a reference to CODE!" unless ref($funToApply) eq "CODE";
+ unless (scalar @$colsToGroupBy) { # all rows are treated as one group
+ $funToApply->($self->clone, 0 .. $self->nofRow - 1);
+ return;
+ }
+ my @X = ();
+ my %grpBy = ();
+ foreach my $x (@$colsToGroupBy) {
+ my $x_idx = $self->checkOldCol($x);
+ confess "Unknown column ". $x unless defined($x_idx);
+ push @X, $x_idx;
+ $grpBy{$x_idx} = 1;
+ }
+ my %X = ();
+ for (my $i=0; $i<$self->nofRow; $i++) {
+ my $myRow = $self->rowRef($i);
+ #my @val = ();
+ #foreach my $x (@X) {
+ # push @val, defined($myRow->[$x])?$myRow->[$x]:"";
+ #}
+ my @val = map {tsvEscape($_)} @{$myRow}[@X];
+ my $myKey = CORE::join("\t", @val);
+ push @{$X{$myKey}}, $i;
+ }
+ foreach my $myKey ( sort {$a cmp $b} keys %X) {
+ $funToApply->($self->subTable($X{$myKey}, undef), $X{$myKey});
+ }
+}
+
+sub group {
+ my ($self, $colsToGroupBy, $colsToCalculate, $funsToApply, $newColNames, $keepRestCols) = @_;
+ $keepRestCols = 1 unless defined($keepRestCols);
+ $colsToGroupBy = [] unless defined $colsToGroupBy;
+ confess "colsToGroupBy has to be specified!" unless defined($colsToGroupBy) && ref($colsToGroupBy) eq "ARRAY";
+ my @X = ();
+ my %grpBy = ();
+ foreach my $x (@$colsToGroupBy) {
+ my $x_idx = $self->checkOldCol($x);
+ confess "Unknown column ". $x unless defined($x_idx);
+ push @X, $x_idx;
+ $grpBy{$x_idx} = 1;
+ }
+ my @Y = ();
+ my %Y= ();
+ if (defined($colsToCalculate)) {
+ foreach my $y (@$colsToCalculate) {
+ my $y_idx = $self->checkOldCol($y);
+ confess "Unknown column ". $y unless defined($y_idx);
+ push @Y, $y_idx;
+ $Y{$y_idx} = 1;
+ }
+ }
+ if (scalar @Y) {
+ confess "The size of colsToCalculate, funcsToApply and newColNames should be the same!\n"
+ unless (scalar @Y == scalar @$funsToApply && scalar @Y == scalar @$newColNames);
+ }
+
+ my @header = ();
+ my @X_name = ();
+ my $cnt = 0;
+ my $i;
+ for ($i=0; $i<$self->nofCol; $i++) {
+ if ($grpBy{$i} || ($keepRestCols && !defined($Y{$i}))) {
+ push @X_name, $i;
+ push @header, $self->{header}->[$i];
+ $cnt += 1;
+ }
+ }
+ if (defined($newColNames)) {
+ foreach my $y (@$newColNames) {
+ push @header, $y;
+ $cnt += 1;
+ }
+ }
+ my @ones = ();
+ my %X = ();
+ my %val = ();
+ my %rowIdx = ();
+ my $idx = 0;
+ for ($i=0; $i<$self->nofRow; $i++) {
+ my @row = ();
+ my $myRow = $self->rowRef($i);
+ my $myKey = '(all)';
+ if (@X) {
+ # if colsToGroupBy is not specified, all rows has myKey = '(all)', therefore treated as one group
+ my @val = map {tsvEscape($_)} @{$myRow}[@X];
+ #foreach my $x (@X) {
+ # push @val, defined($myRow->[$x])?$myRow->[$x]:"";
+ #}
+ $myKey = CORE::join("\t", @val);
+ }
+ if (scalar @Y) {
+ my %Y = ();
+ foreach my $y (@Y) {
+ next if defined($Y{$y});
+ $Y{$y} = 1;
+ if (defined($val{$y}->{$myKey})) {
+ push @{$val{$y}->{$myKey}}, $myRow->[$y];
+ } else {
+ $val{$y}->{$myKey} = [$myRow->[$y]];
+ }
+ }
+ }
+ next if defined($X{$myKey});
+ $X{$myKey} = 1;
+ foreach my $j (@X_name) {
+ push @row, $myRow->[$j];
+ }
+ $row[$cnt-1] = undef if (scalar @row < $cnt);
+ push @ones, \@row;
+ $rowIdx{$myKey} = $idx++;
+ }
+
+ if (scalar @Y) {
+ $cnt -= scalar @Y;
+ for($i=0; $i<scalar @Y; $i++) {
+ foreach my $s (keys %X) {
+ if (ref($funsToApply->[$i]) eq "CODE") {
+ $ones[$rowIdx{$s}]->[$cnt+$i] = $funsToApply->[$i]->(@{$val{$Y[$i]}->{$s}});
+ } else {
+ $ones[$rowIdx{$s}]->[$cnt+$i] = scalar @{$val{$Y[$i]}->{$s}};
+ #confess "The ${i}th element in the function array is not a valid reference!\n";
+ }
+ }
+ }
+ }
+
+ return new Data::Table(\@ones, \@header, 0);
+}
+
+sub pivot {
+ my ($self, $colToSplit, $colToSplitIsStringOrNumeric, $colToFill, $colsToGroupBy, $keepRestCols) = @_;
+ $keepRestCols = 0 unless defined($keepRestCols);
+ $colToSplitIsStringOrNumeric = 0 unless defined($colToSplitIsStringOrNumeric);
+ $colsToGroupBy = [] unless defined $colsToGroupBy;
+ my $y = undef;
+ $y = $self->checkOldCol($colToSplit) if defined $colToSplit;
+ my $y_name = defined($y)?$self->{header}->[$y]:undef;
+ confess "Unknown column ". $colToSplit if (!defined($y) && defined($colToSplit));
+ my $z = undef;
+ $z = $self->checkOldCol($colToFill) if defined($colToFill);
+ my $z_name = defined($z)?$self->{header}->[$z]:undef;
+ confess "Unknown column ". $colToFill if (!defined($z) && defined($colToFill));
+ #confess "Cannot take colToFill, if colToSplit is 'undef'" if (defined($z) && !defined($y));
+ my @X = ();
+ if (defined($colsToGroupBy)) {
+ foreach my $x (@$colsToGroupBy) {
+ my $x_idx = $self->checkOldCol($x);
+ confess "Unknown column ". $x unless defined($x_idx);
+ push @X, $self->{header}->[$x_idx];
+ }
+ }
+ my (@Y, %Y);
+
+ if (defined($colToSplit)) {
+ @Y = $self->col($y);
+ %Y = ();
+ foreach my $val (@Y) {
+ $val = "NULL" unless defined($val);
+ $Y{$val} = 1;
+ }
+ } else {
+ @Y = ('(all)') x $self->nofCol;
+ %Y = ('(all)' => 1);
+ $colToSplitIsStringOrNumeric = 1;
+ }
+ if ($colToSplitIsStringOrNumeric == 0) {
+ foreach my $y (keys %Y) {
+ if ($y =~ /\D/) {
+ $colToSplitIsStringOrNumeric = 1;
+ last;
+ }
+ }
+ }
+ if ($colToSplitIsStringOrNumeric) {
+ @Y = sort { $a cmp $b } (keys %Y);
+ } else {
+ @Y = sort { $a <=> $b } (keys %Y);
+ }
+
+ my @header = ();
+ my $i;
+ my @X_name = ();
+
+ if (!$keepRestCols) {
+ foreach my $x (@X) {
+ push @X_name, $x;
+ }
+ } else {
+ for ($i=0; $i<$self->nofCol; $i++) {
+ next if ((defined($y) && $i==$y) || (defined($z) && $i==$z));
+ push @X_name, $self->{header}->[$i];
+ }
+ }
+ my $cnt = 0;
+ for ($i=0; $i < @X_name; $i++) {
+ my $s = $X_name[$i];
+ while (defined($Y{$s})) {
+ $s = "_".$s;
+ }
+ push @header, $s;
+ $Y{$s} = $cnt++;
+ }
+
+ #if (defined($y)) {
+ foreach my $val (@Y) {
+ push @header, ($colToSplitIsStringOrNumeric?"":"$y_name=") . $val;
+ $Y{$val} = $cnt++;
+ }
+ #}
+
+ my @ones = ();
+ my %X = ();
+ my $rowIdx = 0;
+ for ($i=0; $i<$self->nofRow; $i++) {
+ my @row = ();
+ my $myRow = $self->rowHashRef($i);
+ my $myKey = '(all)'; # set to '' to work with total agreegation (group all rows into one)
+ if (scalar @X) {
+ my @val = ();
+ foreach my $x (@X) {
+ push @val, tsvEscape($myRow->{$x});
+ }
+ $myKey = CORE::join("\t", @val);
+ }
+ unless (defined($X{$myKey})) {
+ foreach my $s (@X_name) {
+ push @row, $myRow->{$s};
+ }
+ for (my $j = scalar @row; $j<$cnt; $j++) {
+ $row[$j] = undef;
+ }
+ #$row[$cnt-1] = undef if (scalar @row < $cnt);
+ }
+ #if (defined($y)) {
+ my $val = defined($y) ? $myRow->{$y_name} : "(all)";
+ $val = "NULL" unless defined($val);
+ if (!defined($X{$myKey})) {
+ $row[$Y{$val}] = defined($z)?$myRow->{$z_name}: $row[$Y{$val}]+1;
+ } else {
+ $ones[$X{$myKey}][$Y{$val}] = defined($z)?$myRow->{$z_name}: $ones[$X{$myKey}][$Y{$val}]+1;
+ }
+ #}
+ unless (defined($X{$myKey})) {
+ push @ones, \@row;
+ $X{$myKey} = $rowIdx++;
+ }
+ }
+ return new Data::Table(\@ones, \@header, 0);
+}
+
+sub fromFileGuessOS {
+ my ($name, $arg_ref) = @_;
+ my @OS=("\n", "\r\n", "\r");
+ # operatoring system: 0 for UNIX (\n as linebreak), 1 for Windows
+ # (\r\n as linebreak), 2 for MAC (\r as linebreak)
+ my $qualifier = '';
+ my $encoding = $Data::Table::DEFAULTS{ENCODING};
+ $qualifier = $arg_ref->{qualifier} if (defined($arg_ref) && exists $arg_ref->{qualifier});
+ $encoding = $arg_ref->{encoding} if (defined($arg_ref) && exists $arg_ref->{encoding});
+ my ($len, $os)=(-1, -1);
+ my $SRC=openFileWithEncoding($name, $encoding);
+ #local($/)="\n";
+ my $s = getOneLine($SRC, "\n", $qualifier); #<$SRC>;
+ close($SRC);
+ #$s =~ s/\n$//;
+ #my $myLen=length($s);
+ #$s =~ s/\r$//;
+ if ($s =~ /\r\n$/) {
+ return 1;
+ } elsif ($s =~ /\n$/) {
+ return 0;
+ } elsif ($s =~ /\r/) {
+ return 2;
+ }
+ return 0;
+ #if (length($s) == $myLen) {
+ # return 0;
+ #} elsif (length($s) == $myLen - 1) {
+ # return 1;
+ #} else {
+ # return 2;
+ #}
+# for (my $i=0; $i<@OS; $i++) {
+# open($SRC, $name) or confess "Cannot open $name to read";
+# binmode $SRC;
+# local($/)=$OS[$i];
+# my $s = <$SRC>;
+# #print ">> $i => ". (length($s)-length($OS[$i]))."\n";
+# my $myLen=length($s)-length($OS[$i]);
+# if ($len<0 || ($myLen>0 && $myLen<$len)) {
+# $len=length($s)-length($OS[$i]);
+# $os=$i;
+# }
+# close($SRC);
+# }
+# # find the OS linebreak that gives the shortest first line
+# return $os;
+}
+
+sub openFileWithEncoding {
+ my ($name_or_handler, $encoding) = @_;
+ my $isFileHandler=ref($name_or_handler) ne "";
+ my $SRC;
+ if ($isFileHandler) {
+ $SRC = $name_or_handler; # a file handler
+ } else {
+ open($SRC, $name_or_handler) or confess "Cannot open $name_or_handler to read";
+ }
+ # check if Perl version is recent enough to support encoding
+ $encoding ='' if (!$^V or $^V lt v5.8.1);
+ if ($encoding) {
+ $encoding='UTF-8' if ($encoding =~ /^utf-?8$/i);
+ binmode($SRC, ":encoding($encoding)");
+ } else {
+ binmode $SRC;
+ }
+ return $SRC;
+}
+
+sub fromFileGetTopLines {
+ my ($name, $os, $numLines, $arg_ref) = @_;
+ $os = fromFileGuessOS($name) unless defined($os);
+ $numLines = 2 unless defined($numLines);
+ my @OS=("\n", "\r\n", "\r");
+ # operatoring system: 0 for UNIX (\n as linebreak), 1 for Windows
+ # (\r\n as linebreak), 2 for MAC (\r as linebreak)
+ my $encoding = $Data::Table::DEFAULTS{ENCODING};
+ $encoding = $arg_ref->{encoding} if (defined($arg_ref) && exists $arg_ref->{encoding});
+ my @lines=();
+ my $SRC = openFileWithEncoding($name, $encoding);
+ local($/)=$OS[$os];
+ my $n_endl = length($OS[$os]);
+ my $cnt=0;
+ while(my $line = <$SRC>) {
+ $cnt++;
+ for (1..$n_endl) { chop($line); }
+ push @lines, $line;
+ last if ($numLines>0 && $cnt>=$numLines);
+ }
+ close($SRC);
+ return @lines;
+}
+
+sub fromFileIsHeader {
+ my ($s, $delimiter, $allowNumericHeader) = @_;
+ $delimiter=$Data::Table::DEFAULTS{'CSV_DELIMITER'} unless defined($delimiter);
+ return 0 if (!defined($s) || $s eq "" || $s=~ /$delimiter$/);
+ my $fields=parseCSV($s, 0, {delimiter=>$delimiter});
+ my $allNumbers = 1;
+ foreach my $name (@$fields) {
+ return 0 unless $name;
+ #next if $name=~/[^0-9.eE\-+]/;
+ return 0 if $name=~/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/ && !$allowNumericHeader;
+ # modified, so that we allow some columns to be numeric, but not all columns
+ $allNumbers = 0 unless $name =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
+ }
+ #return 0 if $allNumbers;
+ return 1;
+}
+
+sub fromFileGuessDelimiter {
+ my $s_line= shift;
+ my @DELIMITER=(",","\t",":");
+ my $numCol=-1; my $i=-1;
+ return $Data::Table::DEFAULTS{CSV_DELIMITER} unless @$s_line;
+ for (my $d=0; $d<@DELIMITER; $d++) {
+ my $colFound=-1;
+ foreach my $line (@$s_line) {
+ unless (defined($line)) {
+ return $Data::Table::DEFAULTS{CSV_DELIMITER};
+ } else {
+ my $header = parseCSV($line, 0, {delimiter=>$DELIMITER[$d]});
+ if ($colFound<0) {
+ $colFound = scalar @$header;
+ } elsif ($colFound != scalar @$header) {
+ $colFound = -1;
+ last;
+ }
+ }
+ }
+ next if $colFound<0;
+ if ($colFound>$numCol) {
+ $numCol=$colFound; $i=$d;
+ }
+ }
+ return ($i<0)?$Data::Table::DEFAULTS{CSV_DELIMITER}:$DELIMITER[$i];
+}
+
+sub fromFile {
+ my ($name, $arg_ref) = @_;
+ my $linesChecked = 2;
+ my $os = undef;
+ my $hasHeader = undef;
+ my $delimiter = undef;
+ my $format = undef;
+ my $qualifier = $Data::Table::DEFAULTS{CSV_QUALIFIER};
+ my $allowNumericHeader = 0;
+ my $encoding=$Data::Table::DEFAULTS{ENCODING};
+
+ if (defined($arg_ref)) {
+ $linesChecked = $arg_ref->{'linesChecked'} if defined($arg_ref->{'linesChecked'});
+ $os = $arg_ref->{'OS'};
+ $hasHeader = $arg_ref->{'has_header'};
+ $delimiter = $arg_ref->{'delimiter'};
+ $format = $arg_ref->{'format'};
+ $qualifier = $arg_ref->{'qualifier'} if defined($arg_ref->{'qualifier'});
+ $allowNumericHeader = $arg_ref->{'allowNumericHeader'};
+ $encoding = $arg_ref->{'encoding'};
+ }
+
+ $qualifier = '' if ($format and uc($format) eq 'TSV');
+ unless (defined($os)) {
+ $os = fromFileGuessOS($name, {qualifier=>$qualifier, encoding=>$encoding});
+ $arg_ref->{'OS'}=$os;
+ }
+ my @S = fromFileGetTopLines($name, $os, $linesChecked, {encoding=>$encoding});
+ return undef unless scalar @S;
+ unless (defined($delimiter)) {
+ $delimiter = fromFileGuessDelimiter(\@S);
+ $arg_ref->{'delimiter'} = $delimiter;
+ }
+ unless (defined($hasHeader)) {
+ $hasHeader = fromFileIsHeader($S[0], $delimiter, $allowNumericHeader);
+ }
+ my $t = undef;
+ #print ">>>". join("\n", @S)."\n";
+ #print "OS=$os, hasHeader=$hasHeader, delimiter=$delimiter\n";
+ if ($delimiter eq "\t") {
+ $t=fromTSV($name, $hasHeader, undef, $arg_ref);
+ } else {
+ $t=fromCSV($name, $hasHeader, undef, $arg_ref);
+ }
+ return $t;
+}
+
+## interface to GD::Graph
+# use GD::Graph::points;
+# $graph = GD::Graph::points->new(400, 300);
+# $graph->plot([$t->colRef(1), $t->colRef(2)]);
+
+1;
+
+__END__
+
+
+=head1 NAME
+
+Data::Table - Data type related to database tables, spreadsheets, CSV/TSV files, HTML table displays, etc.
+
+=head1 SYNOPSIS
+
+ News: The package now includes "Perl Data::Table Cookbook" (PDF), which may serve as a better learning material.
+ To download the free Cookbook, visit https://sites.google.com/site/easydatabase/
+
+ # some cool ways to use Table.pm
+ use Data::Table;
+
+ $header = ["name", "age"];
+ $data = [
+ ["John", 20],
+ ["Kate", 18],
+ ["Mike", 23]
+ ];
+ $t = Data::Table->new($data, $header, 0); # Construct a table object with
+ # $data, $header, $type=0 (consider
+ # $data as the rows of the table).
+ print $t->csv; # Print out the table as a csv file.
+
+ $t = Data::Table::fromCSV("aaa.csv"); # Read a csv file into a table object
+ ### Since version 1.51, a new method fromFile can automatically guess the correct file format
+ # either CSV or TSV file, file with or without a column header line
+ # e.g.
+ # $t = Data::Table::fromFile("aaa.csv");
+ # is equivalent.
+ print $t->html; # Display a 'portrait' HTML TABLE on web.
+
+ use DBI;
+ $dbh= DBI->connect("DBI:mysql:test", "test", "") or die $DBI::errstr;
+ my $minAge = 10;
+ $t = Data::Table::fromSQL($dbh, "select * from mytable where age >= ?", [$minAge]);
+ # Construct a table form an SQL
+ # database query.
+
+ $t->sort("age", 0, 0); # Sort by col 'age',numerical,ascending
+ print $t->html2; # Print out a 'landscape' HTML Table.
+
+ $row = $t->delRow(2); # Delete the third row (index=2).
+ $t->addRow($row, 4); # Add the deleted row back as fifth row.
+ @rows = $t->delRows([0..2]); # Delete three rows (row 0 to 2).
+ $col = $t->delCol("age"); # Delete column 'age'.
+ $t->addCol($col, "age",2); # Add column 'age' as the third column
+ @cols = $t->delCols(["name","phone","ssn"]);
+ # Delete 3 columns at the same time.
+
+ $name = $t->elm(2,"name"); # Element access
+ $t2=$t->subTable([1, 3..4],['age', 'name']);
+ # Extract a sub-table
+
+ $t->rename("Entry", "New Entry"); # Rename column 'Entry' by 'New Entry'
+ $t->replace("Entry", [1..$t->nofRow()], "New Entry");
+ # Replace column 'Entry' by an array of
+ # numbers and rename it as 'New Entry'
+ $t->swap("age","ssn"); # Swap the positions of column 'age'
+ # with column 'ssn' in the table.
+
+ $t->colMap('name', sub {return uc}); # Map a function to a column
+ $t->sort('age',0,0,'name',1,0); # Sort table first by the numerical
+ # column 'age' and then by the
+ # string column 'name' in ascending
+ # order
+ $t2=$t->match_pattern('$_->[0] =~ /^L/ && $_->[3]<0.2');
+ # Select the rows that matched the
+ # pattern specified
+ $t2=$t->match_pattern_hash('$_{"Amino acid"} =~ /^L-a/ && $_{"Grams \"(a.a.)\""}<0.2'));
+ # use column name in the pattern, method added in 1.62
+ $t2=$t->match_string('John'); # Select the rows that matches 'John'
+ # in any column
+
+ $t2=$t->clone(); # Make a copy of the table.
+ $t->rowMerge($t2); # Merge two tables
+ $t->colMerge($t2);
+
+ $t = Data::Table->new( # create an employ salary table
+ [
+ ['Tom', 'male', 'IT', 65000],
+ ['John', 'male', 'IT', 75000],
+ ['Tom', 'male', 'IT', 65000],
+ ['John', 'male', 'IT', 75000],
+ ['Peter', 'male', 'HR', 85000],
+ ['Mary', 'female', 'HR', 80000],
+ ['Nancy', 'female', 'IT', 55000],
+ ['Jack', 'male', 'IT', 88000],
+ ['Susan', 'female', 'HR', 92000]
+ ],
+ ['Name', 'Sex', 'Department', 'Salary'], 0);
+
+ sub average { # this is an subroutine calculate mathematical average, ignore NULL
+ my @data = @_;
+ my ($sum, $n) = (0, 0);
+ foreach $x (@data) {
+ next unless $x;
+ $sum += $x; $n++;
+ }
+ return ($n>0)?$sum/$n:undef;
+ }
+
+ $t2 = $t->group(["Department","Sex"],["Name", "Salary"], [sub {scalar @_}, \&average], ["Nof Employee", "Average Salary"]);
+ # For each (Department,Sex) pair, calculate the number of employees and average salary
+ $t2 = $t2->pivot("Sex", 0, "Average Salary", ["Department"]);
+ # Show average salary information in a Department by Sex spreadsheet
+
+=head1 ABSTRACT
+
+This perl package uses perl5 objects to make it easy for
+manipulating spreadsheet data among disk files, database, and Web
+publishing.
+
+A table object contains a header and a two-dimensional array of scalars.
+Four class methods Data::fromFile, Data::Table::fromCSV, Data::Table::fromTSV, and Data::Table::fromSQL allow users
+to create a table object from a CSV/TSV file or a database SQL selection in a snap.
+
+Table methods provide basic access, add, delete row(s) or column(s) operations, as well as more advanced sub-table extraction, table sorting,
+record matching via keywords or patterns, table merging, and web publishing.
+Data::Table class also provides a straightforward interface to other
+popular Perl modules such as DBI and GD::Graph.
+
+The most updated version of the Perl Data::Table Cookbook is available at
+ https://sites.google.com/site/easydatabase/
+
+We use Data::Table instead of Table, because Table.pm has already been used inside PerlQt module in CPAN.
+
+=head1 INTRODUCTION
+
+=over 4
+
+A table object has three data members:
+
+=item 1. $data:
+
+a reference to an array of array-references.
+It's basically a reference to a two-dimensional array.
+
+=item 2. $header:
+
+a reference to a string array. The array contains all the column names.
+
+=item 3. $type = 1 or 0.
+
+1 means that @$data is an array of table columns (fields) (column-based);
+0 means that @$data is an array of table rows (records) (row-based);
+
+=back
+
+Row-based/Column-based are two internal implementations for a table object.
+E.g., if a spreadsheet consists of two columns lastname and age.
+In a row-based table, $data = [ ['Smith', 29], ['Dole', 32] ].
+In a column-based table, $data = [ ['Smith', 'Dole'], [29, 32] ].
+
+Two implementations have their pros and cons for different operations.
+Row-based implementation is better for sorting and pattern matching,
+while column-based one is better for adding/deleting/swapping columns.
+
+Users only need to specify the implementation type of the table upon its
+creation via Data::Table::new, and can forget about it afterwards.
+Implementation type of a table should be considered volatile, because
+methods switch table objects from one type into another internally.
+Be advised that row/column/element references gained via table::rowRef,
+table::rowRefs, table::colRef, table::colRefs, or table::elmRef may
+become stale after other method calls afterwards.
+
+For those who want to inherit from the Data::Table class, internal method
+table::rotate is used to switch from one implementation type into another.
+There is an additional internal assistant data structure called
+colHash in our current implementation. This hash
+table stores all column names and their corresponding column index number as
+key-value pairs for fast conversion. This gives users an option to use
+column name wherever a column ID is expected, so that user don't have to use
+table::colIndex all the time. E.g., you may say
+$t->rename('oldColName', 'newColName')
+instead of $t->rename($t->colIndex('oldColName'), 'newColIdx').
+
+=head1 DESCRIPTION
+
+=head2 Field Summary
+
+=over 4
+
+=item data refto_arrayof_refto_array
+
+contains a two-dimensional spreadsheet data.
+
+=item header refto_array
+
+contains all column names.
+
+=item type 0/1
+
+0 is row-based, 1 is column-based, describe the orientation of @$data.
+
+=back
+
+=head2 Package Variables
+
+=over 4
+
+=item $Data::Table::VERSION
+
+=item @Data::Table::OK
+
+see table::match_string, table::match_pattern, and table::match_pattern_hash
+Since 1.62, we recommend you to use $table->{OK} instead, which is a local array reference.
+
+=item @Data::Table::MATCH
+
+see table::match_string, table::match_pattern, and table::match_pattern_hash
+Since 1.67, we return the matched row indices in an array. Data::Table::MATCH is this array reference.
+Here is an example of setting a max price of 20 to all items with UnitPrice > 20.
+
+ $t_product->match_pattern_hash('$_{UnitPrice} > 20');
+ $t_product->setElm($t_product->{MATCH}, 'UnitPrice', 20);
+
+=item %Data::Table::DEFAULTS
+
+Store default settings, currently it contains CSV_DELIMITER (set to ','), CSV_QUALIFER (set to '"'), and OS (set to 0).
+see table::fromCSV, table::csv, table::fromTSV, table::tsv for details.
+
+=back
+
+=head2 Class Methods
+
+Syntax: return_type method_name ( [ parameter [ = default_value ]] [, parameter [ = default_value ]] )
+
+If method_name starts with table::, this is an instance method, it can be used as $t->method( parameters ), where $t is a table reference.
+
+If method_name starts with Data::Table::, this is a class method, it should be called as
+ Data::Table::method, e.g., $t = Data::Table::fromCSV("filename.csv").
+
+Conventions for local variables:
+
+ colID: either a numerical column index or a column name;
+ rowIdx: numerical row index;
+ rowIDsRef: reference to an array of column IDs;
+ rowIdcsRef: reference to an array of row indices;
+ rowRef, colRef: reference to an array of scalars;
+ data: ref_to_array_of_ref_to_array of data values;
+ header: ref to array of column headers;
+ table: a table object, a blessed reference.
+
+=head2 Table Creation
+
+=over 4
+
+=item table Data::Table::new ( $data = [], $header = [], $type = 0, $enforceCheck = 1)
+
+create a new table.
+It returns a table object upon success, undef otherwise.
+$data: points to the spreadsheet data.
+$header: points to an array of column names. Before version 1.69, a column name must have at least one non-digit character. Since verison 1.69, this is relaxed. Although integer and numeric column names can now be accepted, when accessing a column by integer, it is first interpreted as a column name.
+$type: 0 or 1 for row-based/column-based spreadsheet.
+$enforceCheck: 1/0 to turn on/off initial checking on the size of each row/column to make sure the data arguement indeed points to a valid structure.
+In 1.63, we introduce constants Data::Table::ROW_BASED and Data::Table::COL_BASED as synonyms for $type. To create an empty Data::Table, use new Data::Table([], [], Data::Table::ROW_BASED);
+
+=item table table::subTable ($rowIdcsRef, $colIDsRef, $arg_ref)
+
+create a new table, which is a subset of the original.
+It returns a table object.
+$rowIdcsRef: points to an array of row indices (or a true/false row mask array).
+$colIDsRef: points to an array of column IDs.
+The function make a copy of selected elements from the original table.
+Undefined $rowIdcsRef or $colIDsRef is interpreted as all rows or all columns.
+The elements in $colIDsRef may be modified as a side effect before version 1.62, fixed in 1.62.
+If $arg_ref->{useRowMask} is set to 1, $rowIdcsRef is a true/false row mask array, where rows marked as TRUE will be returned. Row mask array is typically the Data::Table::OK set by match_string/match_pattern/match_pattern_hash methods.
+
+=item table table::clone
+
+make a clone of the original.
+It return a table object, equivalent to table::subTable(undef,undef).
+
+=item table Data::Table::fromCSV ($name_or_handler, $includeHeader = 1, $header = ["col1", ... ], {OS=>$Data::Table::DEFAULTS{'OS'}, delimiter=>$Data::Table::DEFAULTS{'CSV_DELIMITER'}, qualifier=>$Data::Table::DEFAULTS{'CSV_QUALIFIER'}, skip_lines=>0, skip_pattern=>undef, encoding=>$Data::Table::DEFAULTS{'ENCODING'}})
+
+create a table from a CSV file.
+return a table object.
+$name_or_handler: the CSV file name or an already opened file handler. If a handler is used, it's not closed upon return. To read from STDIN, use Data::Table::fromCSV(\*STDIN, 1).
+$includeHeader: 0 or 1 to ignore/interpret the first line in the file as column names,
+If it is set to 0, the array in $header is used. If $header is not supplied, the default column names are "col1", "col2", ...
+optional named argument OS specifies under which operating system the CSV file was generated. 0 for UNIX, 1 for PC and 2 for MAC. If not specified, $Data::Table::DEFAULTS{'OS'} is used, which defaults to UNIX. Basically linebreak is defined as "\n", "\r\n" and "\r" for three systems, respectively.
+
+optional name argument delimiter and qualifier let user replace comma and double-quote by other meaningful single characters. <b>Exception</b>: if the delimiter or the qualifier is a special symbol in regular expression, you must escape it by '\'. For example, in order to use pipe symbol as the delimiter, you must specify the delimiter as '\|'.
+
+optional name argument skip_lines let you specify how many lines in the csv file should be skipped, before the data are interpretted.
+
+optional name argument skip_pattern let you specify a regular expression. Lines that match the regular expression will be skipped.
+
+optional name argument encoding let you specify an encoding method of the csv file. This option is added to fromCSV, fromTSV, fromFile since version 1.69.
+
+The following example reads a DOS format CSV file and writes a MAC format:
+
+ $t = Data::Table:fromCSV('A_DOS_CSV_FILE.csv', 1, undef, {OS=>1});
+ $t->csv(1, {OS=>2, file=>'A_MAC_CSV_FILE.csv'});
+ open(SRC, 'A_DOS_CSV_FILE.csv') or die "Cannot open A_DOS_CSV_FILE.csv to read!";
+ $t = Data::Table::fromCSV(\*SRC, 1);
+ close(SRC);
+
+The following example reads a non-standard CSV file with : as the delimiter, ' as the qaulifier
+
+ my $s="col_A:col_B:col_C\n1:2, 3 or 5:3.5\none:'one:two':'double\", single'''";
+ open my $fh, "<", \$s or die "Cannot open in-memory file\n";
+ my $t_fh=Data::Table::fromCSV($fh, 1, undef, {delimiter=>':', qualifier=>"'"});
+ close($fh);
+ print $t_fh->csv;
+ # convert to the standard CSV (comma as the delimiter, double quote as the qualifier)
+ # col_A,col_B,col_C
+ # 1,"2, 3 or 5",3.5
+ # one,one:two,"double"", single'"
+ print $t->csv(1, {delimiter=>':', qualifier=>"'"}); # prints the csv file use the original definition
+
+The following example reads bbb.csv file (included in the package) by skipping the first line (skip_lines=>1), then treats any line that starts with '#' (or space comma) as comments (skip_pattern=>'^\s*#'), use ':' as the delimiter.
+
+ $t = Data::Table::fromCSV("bbb.csv", 1, undef, {skip_lines=>1, delimiter=>':', skip_pattern=>'^\s*#'});
+
+Use the optional name argument encoding to specify file encoding method.
+ $t = Data::Table::fromCSV("bbb.csv", 1, undef, {encoding=>'UTF-8'});
+
+=item table table::fromCSVi ($name, $includeHeader = 1, $header = ["col1", ... ])
+
+Same as Data::Table::fromCSV. However, this is an instant method (that's what 'i' stands for), which can be inherited.
+
+=item table Data::Table::fromTSV ($name, $includeHeader = 1, $header = ["col1", ... ], {OS=>$Data::Table::DEFAULTS{'OS'}, skip_lines=>0, skip_pattern=>undef, transform_element=>1, encoding=>$Data::Table::DEFAULTS{'ENCODING'}})
+
+create a table from a TSV file.
+return a table object.
+$name: the TSV file name or an already opened file handler. If a handler is used, it's not closed upon return. To read from STDIN, use Data::Table::fromTSV(\*STDIN, 1).
+$includeHeader: 0 or 1 to ignore/interpret the first line in the file as column names,
+If it is set to 0, the array in $header is used. If $header is not supplied, the default column names are "col1", "col2", ...
+optional named argument OS specifies under which operating system the TSV file was generated. 0 for UNIX, 1 for P
+C and 2 for MAC. If not specified, $Data::Table::DEFAULTS{'OS'} is used, which defaults to UNIX. Basically linebreak is defined as "\n", "\r\n" and "\r" for three systems, respectively. <b>Exception</b>: if the delimiter or the qualifier is a special symbol in regular expression, you must escape it by '\'. For example, in order to use pipe symbol as the delimiter, you must specify the delimiter as '\|'.
+
+optional name argument skip_lines let you specify how many lines in the csv file should be skipped, before the data are interpretted.
+
+optional name argument skip_pattern let you specify a regular expression. Lines that match the regular expression will be skipped.
+
+optional name argument transform_element let you switch on/off \t to tab, \N to undef (etc.) transformation. See TSV FORMAT for details. However, elements are always transformed when export table to tsv format, because not escaping an element containing a tab will be disasterous.
+
+optional name arugment encoding enables one to provide an encoding method when open the tsv file.
+
+See similar examples under Data::Table::fromCSV;
+
+Note: read "TSV FORMAT" section for details.
+
+=item table table::fromTSVi ($name, $includeHeader = 1, $header = ["col1", ... ])
+
+Same as Data::Table::fromTSV. However, this is an instant method (that's what 'i' stands for), which can be inherited.
+
+=item table Data::Table::fromFile ($file_name, $arg_ref = {linesChecked=>2, allowNumericHeader=>0, encoding=>$Data::Table::DEFAULTS{'ENCODING'}})
+
+create a table from a text file.
+return a table object.
+$file_name: the file name (cannot take a file handler).
+linesChecked: the first number of lines used for guessing the input format. The delimiter will have to produce the same number of columns for these lines. By default only check the first 2 lines, 0 means all lines in the file.
+$arg_ref can take additional parameters, such as OS, has_header, delimiter, transform_element, etc. Encoding allows one to specify encoding methods used to open the file, which defaults to UTF-8.
+
+fromFile is added after version 1.51. It relies on the following new methods to automatically figure out the correct file format in order to call fromCSV or fromTSV internally:
+
+ fromFileGuessOS($file_name, {encoding=>'UTF-8'})
+ returns integer, 0 for UNIX, 1 for PC, 2 for MAC
+ fromFileGetTopLines($file_name, $os, $lineNumber, {encoding=>'UTF-8'}) # $os defaults to fromFileGuessOS($file_name), if not specified
+ returns an array of strings, each string represents each row with linebreak removed.
+ fromFileGuessDelimiter($lineArrayRef) # guess delimiter from ",", "\t", ":";
+ returns the guessed delimiter string.
+ fromFileIsHeader($line_concent, $delimiter, $allowNumericHeader) # $delimiter defaults to $Data::Table::DEFAULTS{'CSV_DELIMITER'}
+ returns 1 or 0.
+
+It first ask fromFileGuessOS to figure out which OS (UNIX, PC or MAC) generated the input file. The fetch the first linesChecked lines using fromFileGetTopLines. It then guesses the best delimiter using fromFileGuessDelimiter, then it checks if the first line looks like a column header row using fromFileIsHeader. Since fromFileGuessOS and fromFileGetTopLines needs to open/close the input file, these methods can only take file name, not file handler. If user specify formatting parameters [...]
+
+fromFileGuessOS finds the linebreak that gives shortest first line (in the priority of UNIX, PC, MAC upon tie).
+fromFileGuessDelimiter works based on the assumption that the correct delimiter will produce equal number of columns for the given rows. If multiple matches, it chooses the delimiter that gives maximum number of columns. If none matches, it returns the default delimiter.
+fromFileIsHeader works based on the assumption that no column header can be empty or numeric values. However, if we allow numeric column names (especially integer column names), set {allowNumericHeader => 1}
+
+=item table Data::Table::fromSQL ($dbh, $sql, $vars)
+
+create a table from the result of an SQL selection query.
+It returns a table object upon success or undef otherwise.
+$dbh: a valid database handler.
+Typically $dbh is obtained from DBI->connect, see "Interface to Database" or DBI.pm.
+$sql: an SQL query string or a DBI::st object (starting in version 1.61).
+$vars: optional reference to an array of variable values,
+required if $sql contains '?'s which need to be replaced
+by the corresponding variable values upon execution, see DBI.pm for details.
+Hint: in MySQL, Data::Table::fromSQL($dbh, 'show tables from test') will also create a valid table object.
+
+Data::Table::fromSQL now can take DBI::st instead of a SQL string. This is introduced, so that variable binding (such as CLOB/BLOB) can be done outside the method, for example:
+
+ $sql = 'insert into test_table (id, blob_data) values (1, :val)';
+ $sth = $dbh->prepare($sql);
+ $sth->bind_param(':val', $blob, {ora_type => SQLT_BIN});
+ Data::Table::fromSQL($dbh, $sth);
+
+=item table Data::Table::fromSQLi ($dbh, $sql, $vars)
+
+Same as Data::Table::fromSQL. However, this is an instant method (that's what 'i' stands for), whic
+h can be inherited.
+
+=back
+
+=head2 Table Access and Properties
+
+=over 4
+
+=item int table::colIndex ($colID)
+
+translate a column name into its numerical position, the first column has index 0 as in as any perl array.
+return -1 for invalid column names.
+
+Since 1.69, we allow integer to be used as a column header. The integer $colID will first be checked against column names, if matched, the corresponding column index is returned. E.g., if column name for the 3rd column is "1", colIndex(1) will return 2 instead of 1! In such case, if one need to access the second column, one has to access it by column name, i.e., $t->col(($t->header)[1]).
+
+=item int table::nofCol
+
+return number of columns.
+
+=item int table::nofRow
+
+return number of rows.
+
+=item int table::lastCol
+
+return the index of the last columns, i.e., nofCol - 1.
+
+=item int table::lastRow
+
+return the index of the last rows, i.e., nofRow - 1; This is syntax sugar.
+
+ # these two are equivalent
+ foreach my $i (0 .. $t->lastRow)
+ foreach my $i (0 .. $t->nofRow - 1)
+
+=item bool table::isEmpty
+
+return whether the table has any column, introduced in 1.63.
+
+=item bool table::hasCol($colID)
+
+returns whether the colID is a table column, introduced in 1.63.
+
+=item bool table::colName($colNumericIndex)
+
+returns the column name for a numeric column index, notice the first column has an index of 0. Introduced in 1.68.
+
+=item scalar table::elm ($rowIdx, $colID)
+
+return the value of a table element at [$rowIdx, $colID],
+undef if $rowIdx or $colID is invalid.
+
+=item refto_scalar table::elmRef ($rowIdx, $colID)
+
+return the reference to a table element at [$rowIdx, $colID], to allow possible modification.
+It returns undef for invalid $rowIdx or $colID.
+
+=item array table::header ($header)
+
+Without argument, it returns an array of column names.
+Otherwise, use the new header.
+
+=item int table::type
+
+return the implementation type of the table (row-based/column-based) at the time,
+be aware that the type of a table should be considered as volatile during method calls.
+
+=back
+
+=head2 Table Formatting
+
+=over 4
+
+=item string table::csv ($header, {OS=>$Data::Table::DEFAULTS{'OS'}, file=>undef, delimiter=>$Data::Table::DEFAULTS{'CSV_DELIMITER'}, qualifier=>$Data::Table::DEFAULTS{'CSV_QAULIFIER'}})
+
+return a string corresponding to the CSV representation of the table.
+$header controls whether to print the header line, 1 for yes, 0 for no.
+optional named argument OS specifies for which operating system the CSV file is generated. 0 for UNIX, 1 for P
+C and 2 for MAC. If not specified, $Data::Table::DEFAULTS{'OS'} is used. Basically linebreak is defined as "\n", "\r\n" and "\r" for three systems, respectively.
+if 'file' is given, the csv content will be written into it, besides returning the string.
+One may specify custom delimiter and qualifier if the other than default are desired.
+
+=item string table::tsv
+
+return a string corresponding to the TSV representation of the table.
+$header controls whether to print the header line, 1 for yes, 0 for no.
+optional named argument OS specifies for which operating system the TSV file is generated. 0 for UNIX, 1 for P
+C and 2 for MAC. If not specified, $Data::Table::DEFAULTS{'OS'} is used. Basically linebreak is defined as "\n", "\r\n" and "\r" for three systems, respectively.
+if 'file' is given, the tsv content will be written into it, besides returning the string.
+
+Note: read "TSV FORMAT" section for details.
+
+=item string table::html ($colorArrayRef_or_colorHashRef = ["#D4D4BF","#ECECE4","#CCCC99"],
+ $tag_tbl = {border => '1'},
+ $tag_tr = {align => 'left'},
+ $tag_th = {align => 'center'},
+ $tag_td = {col3 => 'align="right" valign="bottom"', 4 => 'align="left"'},
+ $l_portrait = 1
+ )
+
+return a string corresponding to a 'Portrait/Landscape'-style html-tagged table.
+$colorArrayRef_or_colorHashRef: If a hash reference is provided, it will take three CSS class names for odd data rows, even data rows and for the header row. The default hash is {even=>"data_table_even", odd=>"data_table_odd", header=>"data_table_header").
+If a hash reference is not found, a reference to an array of three color strings is expected to provided for backgrounds for even-row records, odd-row records, and -der row, respectively. A default color array ("#D4D4BF","#ECECE4","#CCCC99") will be used if $colors isn't defined.
+
+Before version 1.59, the parameter can only accept an array reference.
+
+$tag_tbl: a reference to a hash that specifies any legal attributes such as name, border,
+id, class, etc. for the TABLE tag.
+
+$tag_tr: a reference to a hash that specifies any legal attributes for the TR tag.
+
+$tag_th: a reference to a hash that specifies any legal attributes for the TH tag.
+
+$tag_td: a reference to a hash that specifies any legal attributes for the TD tag.
+
+Notice $tag_tr and $tag_th controls all the rows and columns of the whole table. The keys of the hash are the attribute names in these cases. However, $tag_td is column specific, i.e., you should specify TD attributes for every column separately.
+The key of %$tag_td are either column names or column indices, the value is the full string to be inserted into the TD tag. E.g., $tag_td = {col3 => 'align=right valign=bottom} only change the TD tag in "col3" to be <TD align=right valign=bottom>.
+
+$portrait controls the layout of the table. The default is 1, i.e., the table is shown in the
+"Portrait" style, like in Excel. 0 means "Landscape". Since version 1.59, tbody and thead tags are added to the portrait mode output.
+
+Attention: You will have to escape HTML-Entities yourself (for example '<' as '<'), if you have characters in you table which need to be escaped. You can do this for example with the escapeHTML-function from CGI.pm (or the HTML::Entities module).
+
+ use CGI qw(escapeHTML);
+ [...]
+ $t->colMap($columnname, sub{escapeHTML($_)}); # for every column, where HTML-Entities occur.
+
+=item string table::html2 ($colors = ["#D4D4BF","#ECECE4","#CCCC99"],
+ $specs = {'name' => '', 'border' => '1', ...})
+
+This method is deprecated. It's here for compatibility. It now simple call html method with $portrait = 0, see previous description.
+
+return a string corresponding to a "Landscape" html-tagged table.
+This is useful to present a table with many columns, but very few entries.
+Check the above table::html for parameter descriptions.
+
+=back
+
+=head2 Table Operations
+
+=over 4
+
+=item int table::setElm ($rowIdx, $colID, $val)
+
+modify the value of a table element at [$rowIdx, $colID] to a new value $val.
+It returns 1 upon success, undef otherwise.
+In 1.68, setElm can manipulate multiple elements, i.e., $rowIdx and $colIdx can be references to an index array, and setElm() will modifies all cells defined by the grid.
+
+ $t->setElm([0..2], ['ColA', 'ColB'], 'new value');
+ $t->setElm(0, [1..2], 'new value');
+
+ # puts a limit on the price of all expensive items
+ $t_product->match_pattern_hash('$_{UnitPrice} > 20');
+ $t_product->setElm($t_product->{MATCH}, 'UnitPrice', 20);
+
+=item int table::addRow ($rowRef, $rowIdx = table::nofRow, $arg_ref = {addNewCol => 0})
+
+add a new row ($rowRef may point to the actual list of scalars, or it can be a hash_ref (supported since version 1.60)). If $rowRef points to a hash, the method will lookup the value of a field by ts column name: $rowRef->{colName}, if not found, undef is used for that field.
+The new row will be referred as $rowIdx as the result. E.g., addRow($aRow, 0) will put the new row as the very first row. By default, it appends a row to the end.
+In 1.67, we support {addNewCol => 1}, if specified, a new column will be automatically created for each new element encountered in the $rowRef.
+
+ # automatically add a new column "aNewColumn" to $t, in order to hold the new value
+ $t->addRow({anExistingColumn => 123, aNewColumn => "XYZ"}, undef, {addNewCol => 1});
+ # $t only had one column, after this call, it will contain a new column 'col2', in order to hold the new value
+ $t->addRow([123, "XYZ"], undef, {addNewCol => 1});
+
+It returns 1 upon success, undef otherwise.
+
+=item refto_array table::delRow ( $rowIdx )
+
+delete a row at $rowIdx. It will the reference to the deleted row.
+
+=item refto_array table::delRows ( $rowIdcsRef )
+
+delete rows in @$rowIdcsRef. It will return an array of deleted rows in the same order of $rowIdcsRef upon success.
+upon success.
+
+=item int table::addCol ($colRef, $colName, $colIdx = numCol)
+
+add a new column ($colRef points to the actual data), the new column will be referred as $colName or $colIdx as the result. E.g., addCol($aCol, 'newCol', 0) will put the new column as the very first column.
+By default, append a column to the end.
+It will return 1 upon success or undef otherwise.
+In 1.68, $colRef can be a scalar, which is the default value that can be used to create the new column. E.g., to create a new column with default value of undef, 0, 'default', respectively, one can do:
+
+ $t->addCol(undef, 'NewCol');
+ $t->addCol(0, 'NewIntCol');
+ $t->addCol('default', 'NewStringCol');
+
+
+=item refto_array table::delCol ($colID)
+
+delete a column at $colID
+return the reference to the deleted column.
+
+=item arrayof_refto_array table::delCols ($colIDsRef)
+
+delete a list of columns, pointed by $colIDsRef. It will
+return an array of deleted columns in the same order of $colIDsRef upon success.
+
+=item refto_array table::rowRef ($rowIdx)
+
+return a reference to the row at $rowIdx
+upon success or undef otherwise.
+
+=item refto_arrayof_refto_array table::rowRefs ($rowIdcsRef)
+
+return a reference to array of row references upon success, undef otherwise.
+
+=item array table::row ($rowIdx)
+
+return a copy of the row at $rowIdx
+upon success or undef otherwise.
+
+=item refto_hash table::rowHashRef ($rowIdx)
+
+return a reference to a hash, which contains a copy of the row at $rowIdx,
+upon success or undef otherwise. The keys in the hash are column names, and
+the values are corresponding elements in that row. The hash is a copy, therefore modifying the hash values doesn't change the original table.
+
+=item refto_array table::colRef ($colID)
+
+return a reference to the column at $colID
+upon success.
+
+=item refto_arrayof_refto_array table::colRefs ($colIDsRef)
+
+return a reference to array of column references upon success.
+
+=item array table::col ($colID)
+
+return a copy to the column at $colID
+upon success or undef otherwise.
+
+=item int table::rename ($colID, $newName)
+
+rename the column at $colID to a $newName
+(the newName must be valid,
+and should not be identical to any other existing column names).
+It returns 1 upon success
+or undef otherwise.
+
+=item refto_array table::replace ($oldColID, $newColRef, $newName)
+
+replace the column at $oldColID by the array pointed by $newColRef, and renamed it to $newName. $newName is optional if you don't want to rename the column.
+It returns 1 upon success or undef otherwise.
+
+=item int table::swap ($colID1, $colID2)
+
+swap two columns referred by $colID1 and $colID2.
+It returns 1 upon success or undef otherwise.
+
+=item int table::moveCol($colID, $colIdx, $newColName)
+
+move column referred by $colID to a new location $colIdx.
+If $newColName is specified, the column will be renamed as well.
+It returns 1 upon success or undef otherwise.
+
+=item int table::reorder($colIDRefs, $arg_ref)
+
+Rearrange the columns according to the order specified in $colIDRef. Columns not specified in the reference array will be appended to the end!
+If one would like to drop columns not specified, set $arg_ref to {keepRest => 0}.
+reorder() changes the table itself, while subTable(undef, $colIDRefs) will return a new table. reorder() might also runs faster than subTable, as elements may not need to be copied.
+
+=item int table::colMap ($colID, $fun)
+
+foreach element in column $colID, map a function $fun to it.
+It returns 1 upon success or undef otherwise.
+This is a handy way to format a column. E.g. if a column named URL contains URL strings, colMap("URL", sub {"<a href='$_'>$_</a>"}) before html() will change each URL into a clickable hyper link while displayed in a web browser.
+
+=item int table::colsMap ($fun)
+
+foreach row in the table, map a function $fun to it.
+It can do whatever colMap can do and more.
+It returns 1 upon success or undef otherwise.
+colMap function only give $fun access to the particular element per row, while colsMap give $fun full access to all elements per row. E.g. if two columns named duration and unit (["2", "hrs"], ["30", "sec"]). colsMap(sub {$_->[0] .= " (".$_->[1].")"; } will change each row into (["2 hrs", "hrs"], ["30 sec", "sec"]).
+As show, in the $func, a column element should be referred as $_->[$colIndex].
+
+=item int table::sort($colID1, $type1, $order1, $colID2, $type2, $order2, ... )
+
+sort a table in place.
+First sort by column $colID1 in $order1 as $type1, then sort by $colID2 in $order2 as $type2, ...
+$type is 0 for numerical and 1 for others;
+$order is 0 for ascending and 1 for descending;
+
+In 1.62, instead of memorize these numbers, you can use constants instead (notice constants do not start with '$').
+ Data::Table::NUMBER
+ Data::Table::STRING
+ Data::Table::ASC
+ Data::Table::DESC
+
+Sorting is done in the priority of colID1, colID2, ...
+It returns 1 upon success or undef otherwise.
+Notice the table is rearranged as a result! This is different from perl's list sort, which returns a sorted copy while leave the original list untouched,
+the authors feel inplace sorting is more natural.
+
+table::sort can take a user supplied operator, this is useful when neither numerical nor alphabetic order is correct.
+
+ $Well=["A_1", "A_2", "A_11", "A_12", "B_1", "B_2", "B_11", "B_12"];
+ $t = Data::Table->new([$Well], ["PlateWell"], 1);
+ $t->sort("PlateWell", 1, 0);
+ print join(" ", $t->col("PlateWell"));
+ # prints: A_1 A_11 A_12 A_2 B_1 B_11 B_12 B_2
+ # in string sorting, "A_11" and "A_12" appears before "A_2";
+ my $my_sort_func = sub {
+ my @a = split /_/, $_[0];
+ my @b = split /_/, $_[1];
+ my $res = ($a[0] cmp $b[0]) || (int($a[1]) <=> int($b[1]));
+ };
+ $t->sort("PlateWell", $my_sort_func, 0);
+ print join(" ", $t->col("PlateWell"));
+ # prints the correct order: A_1 A_2 A_11 A_12 B_1 B_2 B_11 B_12
+
+=item table table::match_pattern ($pattern, $countOnly)
+
+return a new table consisting those rows evaluated to be true by $pattern
+upon success or undef otherwise. If $countOnly is set to 1, it simply returns the number of rows that matches the string without making a new copy of table. $countOnly is 0 by default.
+
+Side effect: @Data::Table::OK (should use $t->{OK} after 1.62) stores a true/false array for the original table rows. Using it, users can find out what are the rows being selected/unselected.
+Side effect: @Data::Table::MATCH stores a reference to an array containing all row indices for matched rows.
+
+In the $pattern string, a column element should be referred as $_->[$colIndex]. E.g., match_pattern('$_->[0]>3 && $_->[1]=~/^L') retrieve all the rows where its first column is greater than 3 and second column starts with letter 'L'. Notice it only takes colIndex, column names are not acceptable here!
+
+=item table table::match_pattern_hash ($pattern, $countOnly)
+
+return a new table consisting those rows evaluated to be true by $pattern
+upon success or undef otherwise. If $countOnly is set to 1, it simply returns the number of rows that matches the string without making a new copy of table. $countOnly is 0 by default.
+
+Side effect: @Data::Table::OK stores a reference to a true/false array for the original table rows. Using it, users can find out what are the rows being selected/unselected.
+Side effect: @Data::Table::MATCH stores a reference to an array containing all row indices for matched rows.
+
+In the $pattern string, a column element should be referred as ${column_name}.
+match_pattern_hash() is added in 1.62. The difference between this method and match_pattern is each row is fed to the pattern as a hash %_.
+In the case of match_pattern, each row is fed as an array ref $_. The pattern for match_pattern_hash() becomes much cleaner.
+
+If a table has two columns: Col_A as the 1st column and Col_B as the 2nd column, a filter "Col_A>2 AND Col_B<2" is written before as
+ $t->match_pattern('$_->[0] > 2 && $_->[1] <2');
+where we need to figure out $t->colIndex('Col_A') is 0 and $t->colIndex('Col_B') is 1, in order to build the pattern.
+Now you can use column name directly in the pattern:
+ $t->match_pattern_hash('$_{Col_A} >2 && $_{Col_B} <2');
+This method creates $t->{OK}, as well as @Data::Table::OK, same as match_pattern().
+
+=item table table::match_string ($s, $caseIgnore, $countOnly)
+
+return a new table consisting those rows contains string $s in any of its fields upon success, undef otherwise. if $caseIgnore evaluated to true, case will is be ignored (s/$s/i). If $countOnly is set to 1, it simply returns the number of rows that matches the string without making a new copy of table. $countOnly is 0 by default.
+
+Side effect: @Data::Table::OK stores a reference to a true/false array for the original table rows.
+Side effect: @Data::Table::MATCH stores a reference to an array containing all row indices for matched rows.
+Using it, users can find out what are the rows being selected/unselected.
+The $s string is actually treated as a regular expression and
+applied to each row element, therefore one can actually specify several keywords
+by saying, for instance, match_string('One|Other').
+
+=item table table::rowMask($mask, $complement)
+
+mask is reference to an array, where elements are evaluated to be true or false. The size of the mask must be equal to the nofRow of the table. return a new table consisting those rows where the corresponding mask element is true (or false, when complement is set to true).
+
+E.g., $t1=$tbl->match_string('keyword'); $t2=$tbl->rowMask(\@Data::Table::OK, 1) creates two new tables. $t1 contains all rows match 'keyword', while $t2 contains all other rows.
+
+mask is reference to an array, where elements are evaluated to be true or false. The size of the mask must be equal to the nofRow of the table. return
+ a new table consisting those rows where the corresponding mask element is true (or false, when complement is set to true).
+
+E.g., $t1=$tbl->match_string('keyword'); $t2=$tbl->rowMask(\@Data::Table::OK, 1) creates two new tables. $t1 contains all rows match 'keyword', while
+$t2 contains all other rows.
+
+=item table table::iterator({$reverse => 0})
+
+Returns a reference to a enumerator routine, which enables one to loop through each table row. If $reverse is set to 1, it will enumerate backward. The convenience here is each row is fetch as a rowHashRef, so one can easily access row elements by name.
+
+ my $next = $t_product->iterator();
+ while (my $row = $next->()) {
+ # have access to a row as a hash reference, access row number by &$next(1);
+ $t_product->setElm($next->(1), 'ProductName', 'New! '.$row->{ProductName});
+ }
+
+In this example, each $row is fetched as a hash reference, so one can access the elements by $row->{colName}.
+Be aware that the elements in the hash is a copy of the original table elements, so modifying $row->{colName} does not modify the original table. If table modification is intended, one needs to obtain the row index of the returned row. $next->(1) call with a non-empty argument returns the row index of the record that was previously fetched with $next->(). In this example, one uses the row index to modify the original table.
+
+=item table table::each_group($colsToGroupBy, $funsToApply)
+
+Primary key columns are specified in $colsToGroupBy. All rows are grouped by primary keys first (keys sorted as string). Then for each group, subroutines $funToAppy is applied to corresponding rows.
+$funToApply are passed with two parameters ($tableRef, $rowIDsRef). All rows sharing the key are passed in as a Data::Table object (with all columns and in the order of ascending row index) in the first parameter.
+The second optional parameter contains an array of row indices of the group members. Since all rows in the passed-in table contains the same keys, the key value can be obtained from its first table row.
+
+=item table table::group($colsToGroupBy, $colsToCalculate, $funsToApply, $newColNames, $keepRestCols)
+
+Primary key columns are specified in $colsToGroupBy. All rows are grouped by primary keys first. Then for each group, an array of subroutines (in $funsToAppy) are applied to corresponding columns and yield a list of new columns (specified in $newColNames).
+
+$colsToGroupBy, $colsToCalculate are references to array of colIDs. $funsToApply is a reference to array of subroutine references. $newColNames are a
+reference to array of new column name strings. If specified, the size of arrays pointed by $colsToCalculate, $funsToApply and $newColNames should be i
+dentical. A column may be used more than once in $colsToCalculate.
+
+$keepRestCols is default to 1 (was introduced as 0 in 1.64, changed to 1 in 1.66 for backward compatibility) introduced in 1.64), otherwise, the remaining columns are returned with the first encountered value of that group.
+
+E.g., an employee salary table $t contains the following columns: Name, Sex, Department, Salary. (see examples in the SYNOPSIS)
+
+ $t2 = $t->group(["Department","Sex"],["Name", "Salary"], [sub {scalar @_}, \&average], ["Nof Employee", "Average Salary"], 0);
+
+Department, Sex are used together as the primary key columns, a new column "Nof Employee" is created by counting the number of employee names in each group, a new column "Average Salary" is created by averaging the Salary data falled into each group. As the result, we have the head count and average salary information for each (Department, Sex) pair. With your own functions (such as sum, product, average, standard deviation, etc), group method is very handy for accounting purpose.
+If primary key columns are not defined, all records will be treated as one group.
+
+ $t2 = $t->group(undef,["Name", "Salary"], [sub {scalar @_}, \&average], ["Nof Employee", "Average Salary"], 0);
+
+The above statement will output the total number of employees and their average salary as one line.
+
+=item table table::pivot($colToSplit, $colToSplitIsStringOrNumeric, $colToFill, $colsToGroupBy, $keepRestCols)
+
+Every unique values in a column (specified by $colToSplit) become a new column. undef value become "NULL". $colToSplitIsStringOrNumeric is set to numeric (0 or Data::Table:NUMBER), the new column names are prefixed by "oldColumnName=". The new cell element is filled by the value specified by $colToFill (was 1/0 before version 1.63).
+
+Note: yes, it seems I made an incompatible change in version 1.64, where $colToSplitIsStringOrNumber used to be $colToSplitIsNumeric, where 0 meant STRING and 1 meant NUMBER. Now it is opposite. However, I also added auto-type detection code, that this parameter essentially is auto-guessed and most old code should behave the same as before.
+
+When primary key columns are specified by $colsToGroupBy, all records sharing the same primary key collapse into one row, with values in $colToFill filling the corresponding new columns. If $colToFill is not specified, a cell is filled with the number of records fall into that cell.
+
+$colToSplit and $colToFill are colIDs. $colToSplitIsNumeric is 1/0. $colsToGroupBy is a reference to array of colIDs. $keepRestCols is 1/0, by default is 0. If $keepRestCols is off, only primary key columns and new columns are exported, otherwise, all the rest columns are exported as well.
+
+E.g., applying pivot method to the resultant table of the example of the group method.
+
+ $t2->pivot("Sex", 0, "Average Salary",["Department"]);
+
+This creates a 2x3 table, where Departments are use as row keys, Sex (female and male) become two new columns. "Average Salary" values are used to fill the new table elements. Used together with group method, pivot method is very handy for accounting type of analysis.
+If $colsToGroupBy is left as undef, all rows are treated as one group. If $colToSplit is left as undef, the method will generate a column named "(all)" that matches all records share the corresponding primary key.
+
+=item table table::melt($keyCols, $variableCols, $arg_ref)
+
+The idea of melt() and cast() are taken from Hadley Wickham's Reshape package in R language.
+A table is first melt() into a tall-skiny format, where measurements are stored in the format of a variable-value pair per row.
+Such a format can then be easily cast() into various contingency tables.
+
+One needs to specify the columns consisting of primary keys, columns that are consider as variable columns. The output variable column is named 'variable' unless specified by $arg_ref{variableColName}. The output value column is named 'value', unless specified in $arg_ref{valueColName}. By default NULL values are not output, unless $arg_ref{skip_NULL} is set to false. By default empty string values are kept, unless one sets skip_empty to `.
+
+ For each object (id), we measure variable x1 and x2 at two time points
+ $t = new Data::Table([[1,1,5,6], [1,2,3,5], [2,1,6,1], [2,2,2,4]], ['id','time','x1','x2'], Data::Table::ROW_BASED);
+ # id time x1 x2
+ # 1 1 5 6
+ # 1 2 3 5
+ # 2 1 6 1
+ # 2 2 2 4
+
+ # melting a table into a tall-and-skinny table
+ $t2 = $t->melt(['id','time']);
+ #id time variable value
+ # 1 1 x1 5
+ # 1 1 x2 6
+ # 1 2 x1 3
+ # 1 2 x2 5
+ # 2 1 x1 6
+ # 2 1 x2 1
+ # 2 2 x1 2
+ # 2 2 x2 4
+
+ # casting the table, &average is a method to calculate mean
+ # for each object (id), we calculate average value of x1 and x2 over time
+ $t3 = $t2->cast(['id'],'variable',Data::Table::STRING,'value', \&average);
+ # id x1 x2
+ # 1 4 5.5
+ # 2 4 2.5
+
+
+=item table table::cast($colsToGroupBy, $colToSplit, $colToSplitIsStringOrNumeric, $colToCalculate, $funToApply)
+
+see melt(), as melt() and cast() are meant to use together.
+
+The table has been melten before. cast() group the table according to primary keys specified in $colsToGroupBy. For each group of objects sharing the same id,
+it further groups values (specified by $colToCalculate) according to unique variable names (specified by $colToSplit). Then it applies subroutine $funToApply to obtain an aggregate value.
+For the output, each unique primary key will be a row, each unique variable name will become a column, the cells are the calculated aggregated value.
+
+If $colsToGroupBy is undef, all rows are treated as within the same group. If $colToSplit is undef, a new column "(all)" is used to hold the results.
+
+ $t = Data::Table->new( # create an employ salary table
+ [
+ ['Tom', 'male', 'IT', 65000],
+ ['John', 'male', 'IT', 75000],
+ ['Tom', 'male', 'IT', 65000],
+ ['John', 'male', 'IT', 75000],
+ ['Peter', 'male', 'HR', 85000],
+ ['Mary', 'female', 'HR', 80000],
+ ['Nancy', 'female', 'IT', 55000],
+ ['Jack', 'male', 'IT', 88000],
+ ['Susan', 'female', 'HR', 92000]
+ ],
+ ['Name', 'Sex', 'Department', 'Salary'], Data::Table::ROW_BASED);
+
+ # get a Department x Sex contingency table, get average salary across all four groups
+ print $t->cast(['Department'], 'Sex', Data::Table::STRING, 'Salary', \&average)->csv(1);
+ Department,female,male
+ IT,55000,73600
+ HR,86000,85000
+ # get average salary for each department
+ print $t->cast(['Department'], undef, Data::Table::STRING, 'Salary', \&average)->csv(1);
+ Department,(all)
+ IT,70500
+ HR,85666.6666666667
+
+ # get average salary for each gender
+ print $t->cast(['Sex'], undef, Data::Table::STRING, 'Salary', \&average)->csv(1);
+ Sex,(all)
+ male,75500
+ female,75666.6666666667
+
+ # get average salary for all records
+ print $t->cast(undef, undef, Data::Table::STRING, 'Salary', \&average)->csv(1);
+ (all)
+ 75555.5555555556
+
+=back
+
+=head2 Table-Table Manipulations
+
+=over 4
+
+=item int table::rowMerge ($tbl, $argRef)
+
+Append all the rows in the table object $tbl to the original rows.
+Before 1.62, the merging table $tbl must have the same number of columns as the original, as well as the columns are in exactly the same order.
+It returns 1 upon success, undef otherwise.
+The table object $tbl should not be used afterwards, since it becomes part of
+the new table.
+
+Since 1.62, you may provide {byName =>1, addNewCol=>1} as $argRef. If byName is set to 1, the columns in in $tbl do not need to be in the same order as they are in the first table, instead the column name is used for the matching. If addNewCol is set to 1, if $tbl contains a new column name that does not already exist in the first table, this new column will be automatically added to the resultant table. Typically, you want to specify there two options simultaneously.
+
+=item int table::colMerge ($tbl, $argRef)
+
+Append all the columns in table object $tbl to the original columns.
+Table $tbl must have the same number of rows as the original.
+It returns 1 upon success, undef otherwise.
+Table $tbl should not be used afterwards, since it becomes part of
+the new table.
+
+Since 1.62, you can specify {renameCol => 1} as $argRef. This is to auto fix any column name collision. If $tbl contains a column that already exists in the first table, it will be renamed (by a suffix _2) to avoid the collision.
+
+=item table table::join ($tbl, $type, $cols1, $cols2, $argRef)
+
+Join two tables. The following join types are supported (defined by $type):
+
+0: inner join
+1: left outer join
+2: right outer join
+3: full outer join
+
+In 1.62, instead of memorize these numbers, you can use constants instead (notice constants do not start with '$').
+ Data::Table::INNER_JOIN
+ Data::Table::LEFT_JOIN
+ Data::Table::RIGHT_JOIN
+ Data::Table::FULL_JOIN
+
+$cols1 and $cols2 are references to array of colIDs, where rows with the same elements in all listed columns are merged. As the result table, columns listed in $cols2 are deleted, before a new table is returned.
+
+The implementation is hash-join, the running time should be linear with respect to the sum of number of rows in the two tables (assume both tables fit in memory).
+
+If the non-key columns of the two tables share the same name, the routine will fail, as the result table cannot contain two columns of the same name. In 1.62, one can specify {renameCol=>1} as $argRef, so that the second column will be automatically renamed (with suffix _2) to avoid collision.
+
+If you would like to treat the NULLs in the key columns as empty string, set {NULLasEmpty => 1}. If you do not want to treat NULLs as empty strings, but you still like the NULLs in two tables to be considered as equal (but not equal to ''), set {matchNULL => 1}. Obviously if NULLasEmpty is set to 1, matchNULL will have no effect.
+
+=back
+
+=head2 Internal Methods
+
+All internal methods are mainly implemented for used by
+other methods in the Table class. Users should avoid using them.
+Nevertheless, they are listed here for developers who
+would like to understand the code and may derive a new class from Data::Table.
+
+=over 4
+
+=item int table::rotate
+
+convert the internal structure of a table between row-based and column-based.
+return 1 upon success, undef otherwise.
+
+=item string csvEscape($string, {delimiter=>, qualifier})
+
+Encode a scalar into a CSV-formatted field.
+
+optional named arguments: delimiter and qualifier, in case user wants to use characters other than the defaults.
+The default delimiter and qualifier is taken from $Data::Table::DEFAULTS{'CSV_DELIMITER'} (defaults to ',') and $Data::Table::DEFAULTS{'CSV_QUALIFIER'} (defaults to '"'), respectively.
+
+Please note that this function only escape one element in a table. To escape the whole table row, you need to
+ join($delimiter, map {csvEscape($_)} @row . $endl;
+$endl refers to End-of-Line, which you may or may not want to add, and it is OS-dependent. Therefore, csvEscape method is kept to the simplest form as an element transformer.
+
+=item refto_array parseCSV($string)
+
+Break a CSV encoded string to an array of scalars (check it out, we did it the cool way).
+
+optional argument size: specify the expected number of fields after csv-split.
+optional named arguments: delimiter and qualifier, in case user wants to use characters other than the defaults.
+respectively. The default delimiter and qualifier is taken from $Data::Table::DEFAULTS{'CSV_DELIMITER'} (defaults to ',') and $Data::Table::DEFAULTS{'CSV_QUALIFIER'} (defaults to '"'), respectively.
+
+=item string tsvEscape($rowRef)
+
+Encode a scalar into a TSV-formatted string.
+
+=back
+
+=head1 TSV FORMAT
+
+There is no standard for TSV format as far as we know. CSV format can't handle binary data very well, therefore, we choose the TSV format to overcome this limitation.
+
+We define TSV based on MySQL convention.
+
+ "\0", "\n", "\t", "\r", "\b", "'", "\"", and "\\" are all escaped by '\' in the TSV file.
+ (Warning: MySQL treats '\f' as 'f', and it's not escaped here)
+ Undefined values are represented as '\N'.
+
+However, you can switch off this transformation by setting {transform_element => 0} in the fromTSV or tsv method.
+Before if a cell reads 'A line break is \n', it is read in as 'A link break is [return]' in memory. When use tsv method to export, it is transformed back to 'A line break is \n'. However, if it is exported as a csv, the [return] will break the format. Now if transform_element is set to 0, the cell is stored as 'A line break is \n' in memory, so that csv export will be correct. However, do remember to set {transform_element => 0} in tsv export method, otherwise, the cell will become 'A li [...]
+
+=head1 INTERFACE TO OTHER SOFTWARES
+
+Spreadsheet is a very generic type, therefore Data::Table class provides an easy
+interface between databases, web pages, CSV/TSV files, graphics packages, etc.
+
+Here is a summary (partially repeat) of some classic usages of Data::Table.
+
+=head2 Interface to Database and Web
+
+ use DBI;
+
+ $dbh= DBI->connect("DBI:mysql:test", "test", "") or die $DBI::errstr;
+ my $minAge = 10;
+ $t = Data::Table::fromSQL($dbh, "select * from mytable where age >= ?", [$minAge]);
+ print $t->html;
+
+=head2 Interface to CSV/TSV
+
+ $t = fromFile("mydata.csv"); # after version 1.51
+ $t = fromFile("mydata.tsv"); # after version 1.51
+
+ $t = fromCSV("mydata.csv");
+ $t->sort(1,1,0);
+ print $t->csv;
+
+ Same for TSV
+
+=head2 Interface to Excel XLS/XLSX
+
+Read in two tables from NorthWind.xls file, writes them out to XLSX format. See Data::Table::Excel module for details.
+
+ use Data::Table::Excel;
+
+ my ($tableObjects, $tableNames)=xls2tables("NorthWind.xls");
+ $t_category = $tableObjects[0];
+ $t_product = $tableObjects[1];
+
+ tables2xlsx("NorthWind.xlsx", [$t_category, $t_product]);
+
+=head2 Interface to Graphics Package
+
+ use GD::Graph::points;
+
+ $graph = GD::Graph::points->new(400, 300);
+ $t2 = $t->match('$_->[1] > 20 && $_->[3] < 35.7');
+ my $gd = $graph->plot($t->colRefs([0,2]));
+ open(IMG, '>mygraph.png') or die $!;
+ binmode IMG;
+ print IMG $gd->png;
+ close IMG;
+
+=head1 AUTHOR
+
+Copyright 1998-2008, Yingyao Zhou & Guangzhou Zou. All rights reserved.
+
+It was first written by Zhou in 1998, significantly improved and maintained by Zou since 1999. The authors thank Tong Peng and Yongchuang Tao for valuable suggestions. We also thank those who kindly reported bugs, some of them are acknowledged in the "Changes" file.
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+Please send bug reports and comments to: easydatabase at gmail dot com. When sending
+bug reports, please provide the version of Table.pm, the version of
+Perl.
+
+=head1 SEE ALSO
+
+ DBI, GD::Graph, Data::Table::Excel.
+
+=cut
+
diff --git a/aaa.csv b/aaa.csv
new file mode 100755
index 0000000..75aad90
--- /dev/null
+++ b/aaa.csv
@@ -0,0 +1,10 @@
+Amino acid,Entry,Solvent,"Grams ""(a.a.)""/100g sol.","Temp, C",Ref No.
+Alanine,1,Ethanol,0.0087,25.0,1
+L-aspartate,2,Ethanol,0.000196,25.0,2
+L-glutamate,3,Ethanol,0.0056,44.93,2
+Glycine,4,Ethanol,0.0037,25.0,1
+L-allo-isoleucine,5,Ethanol,0.19,79,5
+L-isoleucine,6,Ethanol,0.13,79,5
+L-leucine,7,Ethanol,0.217,25.0,1
+L-proline,8,Ethanol,1.5,19,6
+Valiane,9,Ethanol,0.019,25.0,1
diff --git a/aaa.tsv b/aaa.tsv
new file mode 100755
index 0000000..65e16f9
--- /dev/null
+++ b/aaa.tsv
@@ -0,0 +1,10 @@
+Amino acid Entry Solvent Grams \"(a.a.)\"/100g sol. Temp, C Ref No.
+Alanine 1 Ethanol 0.0087 25.0 1
+L-aspartate 2 Ethanol 0.000196 25.0 2
+L-glutamate 3 Ethanol 0.0056 44.93 2
+Glycine 4 Ethanol 0.0037 25.0 1
+L-allo-isoleucine 5 Ethanol 0.19 79 5
+L-isoleucine 6 Ethanol 0.13 79 5
+L-leucine 7 Ethanol 0.217 25.0 1
+L-proline 8 Ethanol 1.5 19 6
+Valiane 9 Ethanol 0.019 25.0 1
diff --git a/bbb.csv b/bbb.csv
new file mode 100755
index 0000000..dec9435
--- /dev/null
+++ b/bbb.csv
@@ -0,0 +1,12 @@
+***** This table is the same as the aaa.csv ****
+ # lines start with # are comment lines
+Amino acid:Entry:Solvent:"Grams ""(a.a.)""/100g sol.":"Temp, C":Ref No.
+Alanine:1:Ethanol:0.0087:25.0:1
+L-aspartate:2:Ethanol:0.000196:25.0:2
+L-glutamate:3:Ethanol:0.0056:44.93:2
+Glycine:4:Ethanol:0.0037:25.0:1
+L-allo-isoleucine:5:Ethanol:0.19:79:5
+L-isoleucine:6:Ethanol:0.13:79:5
+L-leucine:7:Ethanol:0.217:25.0:1
+L-proline:8:Ethanol:1.5:19:6
+Valiane:9:Ethanol:0.019:25.0:1
diff --git a/ccc.csv b/ccc.csv
new file mode 100755
index 0000000..ebf748a
--- /dev/null
+++ b/ccc.csv
@@ -0,0 +1,3 @@
+Department,female,male
+IT,55000,76000
+HR,86000,85000
diff --git a/colon.csv b/colon.csv
new file mode 100755
index 0000000..2001054
--- /dev/null
+++ b/colon.csv
@@ -0,0 +1,3 @@
+col_A:col_B:col_C
+1:2, 3 or 5:3.5
+one:'one:two':'double", single'''
\ No newline at end of file
diff --git a/t/test.t b/t/test.t
new file mode 100755
index 0000000..d94e9b2
--- /dev/null
+++ b/t/test.t
@@ -0,0 +1,341 @@
+BEGIN { $| = 1; }
+use strict;
+use Test::More tests=>81;
+use Data::Table;
+#use Data::Dumper;
+
+#$loaded = 1;
+#print "ok loaded\n";
+
+my $t = Data::Table::fromCSV("aaa.csv");
+ok(defined($t), "fromCSV()");
+ok($t->colIndex('Grams "(a.a.)"/100g sol.') == 3, "colIndex()");
+ok($t->nofCol() == 6, "nofCol()");
+ok($t->nofRow() == 9, "nofRow()");
+ok($t->html() =~ /^<table/i, "html()");
+ok($t->html2() =~ /^<table/i, "html2()");
+ok($t->nofCol() == 6, "nofCol()");
+
+my $fun = sub {return lc;};
+ok($t->colMap(0,$fun)>0, "colMap()");
+$fun = sub {$_->[0] = ucfirst $_->[0]};
+ok($t->colsMap($fun)>0, "colsMap()");
+
+my $row;
+ok(($row = $t->delRow(0)) && $t->nofRow==8, "delRow()");
+ok($t->addRow($row) && $t->nofRow==9, "addRow()");
+
+my @rows;
+ok((@rows = $t->delRows([0,2,3])) && $t->nofRow==6, "delRows()");
+$t->addRow(shift @rows,0);
+$t->addRow(shift @rows,2);
+$t->addRow(shift @rows,3);
+
+my $col;
+ok($t->nofRow==9, "delRows() & addRow()");
+ok(($col = $t->delCol("Solvent")) && $t->nofCol==5, "delCol()");
+ok($t->addCol($col, "Solvent",2) && $t->nofCol==6, "addCol()");
+
+my @cols;
+ok((@cols = $t->delCols(["Temp, C","Amino acid","Entry"])) && $t->nofCol==3, "delCols()");
+$t->addCol(shift @cols,"Temp, C",2);
+$t->addCol(shift @cols,"Amino acid",0);
+$t->addCol(shift @cols,"Entry",1);
+
+ok($t->nofCol==6, "delCols() & addCol()");
+ok($t->rowRef(3), "rowRef()");
+ok($t->rowRefs(undef), "rowRefs()");
+ok($t->row(3), "row()");
+ok($t->colRef(3), "colRef()");
+ok($t->colRefs(["Temp, C", "Amino acid", "Solvent"]), "colRefs()");
+ok($t->col(3), "col()");
+ok($t->rename("Entry", "New Entry"), "rename()");
+$t->rename("New Entry", "Entry");
+
+my @t = $t->col("Entry");
+$t->replace("Entry", [1..$t->nofRow()], "New Entry");
+ok($t->replace("New Entry",\@t, 'Entry'), "replace()");
+
+ok($t->swap("Amino acid","Entry"), "swap()");
+$t->swap("Amino acid","Entry");
+ok($t->elm(3,"Temp, C")==79, "elm()");
+ok(${$t->elmRef(3,"Temp, C")}==79, "elmRef()");
+$t->setElm(3,"Temp, C", 100);
+ok($t->elm(3,"Temp, C")==100, "setElm()");
+$t->setElm(3,"Temp, C",79);
+ok($t->sort('Ref No.',1,1,'Temp, C',1,0), "sort()");
+
+my $t2;
+ok(($t2=$t->match_pattern('$_->[0] =~ /^L-a/ && $_->[3]<0.2')) && $t2->nofRow()==2, "match_pattern()");
+ok(($t2=$t->match_string('allo|cine')) && $t2->nofRow()==4, "match_string()");
+ok($t2=$t->clone(), "clone()");
+ok($t2=$t->subTable([2..4],[0..($t->nofCol()-1)]), "subTable()");
+ok($t2=$t->subTable([2..4],undef), "subTable(\$rowIdcsRef,undef)");
+ok(($t2=$t->subTable(undef,[0..($t->nofCol-1)]))&& ($t2->nofRow() == 9), "subTable(undef,\$colIDsRef)");
+ok($t->rowMerge($t2) && $t->nofRow()==18, "rowMerge()");
+$t->delRows([9..$t->nofRow-1]);
+$t2=$t->subTable([0..($t->nofRow-1)],[1]);
+$t2->rename(0, "new column");
+ok($t->colMerge($t2) && $t->nofCol()==7, "colMerge()");
+$t->delCol('new column');
+$t->sort('Entry',Data::Table::STRING,Data::Table::ASC);
+$t2 = Data::Table::fromTSV("aaa.tsv");
+ok($t->tsv eq $t2->tsv, "fromTSV() and tsv()");
+$t2 = $t->rowHashRef(1);
+ok(scalar keys(%$t2) == $t->nofCol, "rowHashRef()");
+$t2=Data::Table::fromCSV('aaa.csv');
+is_deeply($t->rowRefs(), $t2->rowRefs(), "Looks good so far");
+$t2->rename(0,'New1');
+$t2->rename(1,'New2');
+$t2->rename(2,'New3');
+$t2->rename(3,'New4');
+$t2->rename(4,'New5');
+$t2->rename(5,'New6');
+$t2->delRows([2,3,4]);
+$t->delRows([0,8]);
+
+my $t3 = $t->join($t2, 0, [0,1], [0,1]);
+ok($t3->nofRow == 4, "join: inner");
+$t3 = $t->join($t2, 1, [0,1], [0,1]);
+ok($t3->nofRow == 7, "join: left outer");
+$t3 = $t->join($t2, 2, [0,1], [0,1]);
+ok($t3->nofRow == 6, "join: right outer");
+$t3 = $t->join($t2, 3, [0,1], [0,1]);
+ok($t3->nofRow == 9, "join: full outer");
+
+$t = Data::Table->fromCSVi("aaa.csv");
+$t2=Data::Table::fromCSV('aaa.csv');
+is_deeply($t->rowRefs(), $t2->rowRefs(), "instant method fromCSVi");
+$t = Data::Table->fromTSVi("aaa.tsv");
+is_deeply($t->rowRefs(), $t2->rowRefs(), "instant method fromTSVi");
+
+$t2 = $t->match_string("L-proline");
+$t3 = $t->rowMask(\@Data::Table::OK, 1);
+ok($t2->nofRow == 1 && $t3->nofRow == $t->nofRow - $t2->nofRow, "rowMask()");
+
+my @h = $t2->header;
+my @h2 = @h;
+$h2[1] = "new name";
+$t2->header(\@h2);
+ok($t2->rename("new name", $h[1]), "header rename()");
+
+$t = Data::Table->new(
+ [
+ ['Tom', 'male', 'IT', 65000],
+ ['John', 'male', 'IT', 75000],
+ ['Peter', 'male', 'HR', 85000],
+ ['Mary', 'female', 'HR', 80000],
+ ['Nancy', 'female', 'IT', 55000],
+ ['Jack', 'male', 'IT', 88000],
+ ['Susan', 'female', 'HR', 92000]
+ ],
+ ['Name', 'Sex', 'Department', 'Salary'], 0);
+
+sub average {
+ my @data = @_;
+ my ($sum, $n) = (0, 0);
+ foreach my $x (@data) {
+ next unless $x;
+ $sum += $x; $n++;
+ }
+ return ($n>0)?$sum/$n:undef;
+}
+
+$t2 = $t->group([],["Name", "Salary"], [sub {scalar @_}, \&average], ["Nof Employee", "Average Salary"], 0);
+ok($t2->nofRow == 1 && $t2->elm(0,0) == 7, "group() with no key");
+$t2 = $t->group(["Department","Sex"],["Name", "Salary"], [sub {scalar @_}, \&average], ["Nof Employee", "Average Salary"]);
+ok($t2->nofRow == 4 && $t2->nofCol == 4, "group()");
+
+$t2 = $t2->pivot("Sex", Data::Table::STRING, "Average Salary", ["Department"]);
+#print $t2->html;
+ok($t2->nofRow == 2 && $t2->nofCol == 3, "pivot()");
+
+my $s = $t2->csv;
+#open my $fh, "<", \$s or die "Cannot open in-memory file\n";
+my $fh;
+open($fh, "ccc.csv") or die "Cannot open ccc.csv to read\n";
+my $t_fh=Data::Table::fromCSV($fh);
+close($fh);
+ok($t_fh->csv eq $s, "fromCSV() using file handler");
+#print $t2->csv;
+
+#my $s = $t2->tsv;
+#open my $fh, "<", \$s or die "Cannot open in-memory file\n";
+open($fh, "ccc.csv") or die "Cannot open ccc.csv to read\n";
+$t_fh=Data::Table::fromTSV($fh);
+close($fh);
+ok($t_fh->tsv eq $s, "fromTSV() using file handler");
+#print $t2->csv;
+
+my $Well=["A_1", "A_2", "A_11", "A_12", "B_1", "B_2", "B_11", "B_12"];
+$t = Data::Table->new([$Well], ["PlateWell"], 1);
+$t->sort("PlateWell", 1, 0);
+#print join(" ", $t->col("PlateWell"));
+# in string sorting, "A_11" and "A_12" appears before "A_2";
+my $my_sort_func = sub {
+ my @a = split /_/, $_[0];
+ my @b = split /_/, $_[1];
+ return ($a[0] cmp $b[0]) || (int($a[1]) <=> int($b[1]));
+};
+$t->sort("PlateWell", $my_sort_func, 0);
+#print join(" ", $t->col("PlateWell"));
+#$t->sort("PlateWell", $my_sort_func, 1);
+#print join(" ", $t->col("PlateWell"));
+
+ok(join("", $t->col("PlateWell")) eq join("", @$Well), "sort using custom operator");
+
+#open $fh, "<", \$s or die "Cannot open in-memory file\n";
+open($fh, "colon.csv") or die "Cannot open colon.csv to read\n";
+$t_fh=Data::Table::fromCSV($fh, 1, undef, {delimiter=>':', qualifier=>"'"});
+close($fh);
+ # col_A,col_B,col_C
+ # 1,"2, 3 or 5",3.5
+ # one,one:two,"double"", single'"
+ok($t_fh->elm(0, 'col_B') eq "2, 3 or 5"
+ && $t_fh->elm(1, 'col_B') eq "one:two"
+ && $t_fh->elm(1, 'col_C') eq 'double", single\'',
+ "using custom delimiter and qualifier for fromCSV()");
+
+$t = Data::Table::fromCSV("bbb.csv", 1, undef, {skip_lines=>1, delimiter=>':', skip_pattern=>'^\s*#'});
+$s = $t->tsv;
+$t2 = Data::Table::fromTSV("aaa.tsv", 1);
+is_deeply($t->rowRefs, $t2->rowRefs, "using skip_lines and skip_pattern for fromCSV()");
+
+$t=Data::Table::fromFile("ttt.tsv", {transform_element=>0});
+$t2=Data::Table::fromFile("ttt.csv");
+is_deeply($t->rowRefs(), $t2->rowRefs(), "using fromFile, fromTSV, transform_element");
+
+ok($t->html({odd=>'myOdd', even=>'myEven', header=>'myHeader'}), "using html with CSS class");
+
+my %myRow=(COL_B=>'xyz');
+$t->addRow(\%myRow, 1);
+if ($t->nofRow==3) {
+ is_deeply($t->rowRef(1), [undef, 'xyz'], "addRow() with hash_ref");
+} else {
+ ok(0, "addRow() with hash_ref, row was not added.");
+}
+#ok($t->addRow(\%myRow, 1) && $t->nofRow==3 && equal([$t->rowRef(1)], [[undef, 'xyz']]), "addRow() with hash_ref");
+
+$t2 = $t->clone();
+map {$t2->rename($_, $_."2")} $t2->header;
+$t->rowMerge($t2, {byName => 1});
+ok($t->nofRow == $t2->nofRow*2 && $t->nofCol == $t2->nofCol, "rowMerge() with byName=1");
+
+$t->rowMerge($t2, {byName => 1, addNewCol => 1});
+ok($t->nofRow == $t2->nofRow*3 && $t->nofCol == $t2->nofCol*2, "rowMerge() with byName=1 and addNewCol=1");
+
+$t2->rename(0, 'COL_A');
+$t2->rename(1, 'COL_B');
+$t->rowMerge($t2, {byName => 0, addNewCol => 1});
+ok($t->nofRow == $t2->nofRow*4 && $t->nofCol == $t2->nofCol, "rowMerge() with byName=0 and addNewCol=1");
+
+$t=Data::Table::fromCSV("aaa.csv", 1);
+$t2=$t->clone();
+$t = $t->join($t2, 0, ['Amino acid'], ['Amino acid'], {renameCol => 1});
+ok($t->nofRow == $t2->nofRow && $t->nofCol == $t2->nofCol*2-1, "join() with auto renaming duplicate column names");
+
+$t=Data::Table::fromCSV("aaa.csv", 1);
+$t2=$t->clone();
+$t->colMerge($t2, {renameCol => 1});
+ok($t->nofCol == $t2->nofCol*2, "colMerge() with auto renaming duplicate column names");
+
+$t=Data::Table::fromCSV("aaa.csv", 1);
+ok(($t2=$t->match_pattern_hash('$_{"Amino acid"} =~ /^L-a/ && $_{"Grams \"(a.a.)\"/100g sol."}<0.2')) && $t2->nofRow()==2, "match_pattern_hash()");
+
+$t2 = $t->subTable($t->{OK}, undef, {useRowMask=>1});
+#print Dumper($t2);
+ok($t2->nofRow()==2, "subTable() with row mask");
+
+$t2->moveCol('Amino acid', 1);
+ok(($t2->header)[1] eq 'Amino acid', "moveCol()");
+
+#Entry,Amino acid,Solvent,"Grams ""(a.a.)""/100g sol.","Temp, C",Ref No.
+$t2->reorder(["Amino acid","Temp, C","Entry"]);
+ok(($t2->header)[1] eq 'Temp, C', "reorder()");
+
+$t = Data::Table->new([[1,1,5,6], [1,2,3,5], [2,1,6,1], [2,2,2,4]],
+ ['id','time','x1','x2'], Data::Table::ROW_BASED);
+
+$t2=Data::Table->new([],['id','count', 'rows']);
+$t->each_group(['id'], sub { my ($t, $rows) = @_; $t2->addRow([$t->elm(0,'id'), $t->nofRow, join(":", @$rows)])});
+$t3 = Data::Table->new([[1,2], [2,2], ['0:1','2:3']], ['id','count','rows'], Data::Table::COL_BASED);
+is_deeply($t2->rowRefs, $t3->rowRefs, "group_each()");
+
+$t2 = $t->melt(['id','time']);
+ok($t2->nofRow == 8 && $t2->nofCol == 4, "melt()");
+
+$t3 = $t2->cast(['id'],'variable',Data::Table::STRING,'value', \&average);
+$t=Data::Table->new([[1,4,5.5], [2,4,2.5]], ['id','x1','x2'], Data::Table::ROW_BASED);
+is_deeply($t3->rowRefs, $t->rowRefs, "cast()");
+
+$t3 = $t2->cast(['id'],undef,Data::Table::STRING,'value', \&average);
+$t=Data::Table->new([[1,4.75], [2,3.25]], ['id','(all)'], Data::Table::ROW_BASED);
+is_deeply($t3->rowRefs, $t->rowRefs, "cast() without column to split");
+
+$t3 = $t2->cast(undef,undef,Data::Table::STRING,'value', \&average);
+$t=Data::Table->new([[4]], ['(all)'], Data::Table::ROW_BASED);
+is_deeply($t3->rowRefs, $t->rowRefs, "cast() with total aggregate");
+
+my $t_product=Data::Table::fromFile("Product.csv");
+$t_product->match_pattern_hash('$_{UnitPrice} > 20');
+$t_product->addCol('No', 'IsExpensive');
+ok($t_product->lastCol == 6 && $t_product->elm(0, 'IsExpensive') eq 'No', "addCol() with default value");
+
+ok(@{$t_product->{MATCH}} == 37, "{MATCH} after match_pattern_hash");
+
+$t_product->setElm($t_product->{MATCH}, 'IsExpensive', 'Yes');
+ok($t_product->elm($t_product->{MATCH}->[0], 'IsExpensive') eq 'Yes',
+ "setElm() for multiple cells");
+
+my $cnt = 0;
+my $next = $t_product->iterator();
+while (my $row = &$next) {
+ $cnt ++;
+ $t_product->setElm(&$next(1), 'ProductName', 'New! '.$row->{ProductName});
+}
+
+ok($cnt == 77 && $t_product->elm(0, 'ProductName') =~ /^New!/,
+ "iterator()");
+
+$t_product->addRow({NewColumn=>'xyz',CategoryName=>'myname'}, undef, {addNewCol=>1});
+ok($t_product->hasCol('NewColumn') && $t_product->elm($t_product->lastRow, 'NewColumn') eq 'xyz',
+ "addRow() that adds a column");
+
+# use DBI;
+# $dbh= DBI->connect("DBI:mysql:test", "test", "") or die $dbh->errstr;
+# $t = Data::Table::fromSQL($dbh, "show tables");
+# print $t->csv;
+# $t = Data::Table->fromSQLi($dbh, "show tables");
+# print $t->csv;
+
+# @_ in match_
+package FOO;
+our @ISA = qw(Data::Table);
+
+1;
+
+package main;
+
+my $foo=FOO->new([[11,12],[21,22],[31,32]],['header1','header2'],0);
+ok($foo->csv ne '', "Inheritance");
+
+$foo = FOO->fromCSVi("aaa.csv");
+ok($foo->csv ne '', "inheritated instant method fromCSVi");
+
+# no longer needed, use is_deep instead
+#sub equal {
+# return is_deeply($data, $data2);
+# my ($data, $data2) = @_;
+# my ($i ,$j);
+# return 0 if (scalar @$data != scalar @$data2);
+# for ($i=0; $i< scalar @$data; $i++) {
+# return 0 if (scalar @{$data->[$i]} != scalar @{$data2->[$i]});
+# for ($j=0; $j< scalar @{$data->[0]}; $j++) {
+# if (!defined($data->[$i]->[$j]) || !defined($data2->[$i]->[$j])) {
+# return 0 if (defined($data->[$i]->[$j]) || defined($data2->[$i]->[$j]));
+# }
+# return 0 if ("".$data->[$i]->[$j] ne "".$data2->[$i]->[$j]);
+# }
+# }
+# return 1;
+#}
diff --git a/t/test1.t b/t/test1.t
new file mode 100755
index 0000000..765ea4d
--- /dev/null
+++ b/t/test1.t
@@ -0,0 +1,17 @@
+use strict;
+use Data::Table;
+use Test::More tests => 5;
+
+is(Data::Table::fromFileGuessOS("t_unix.csv"), 0, "Guess Unix file format");
+#is(Data::Table::fromFileGuessOS("t_dos.csv"), 1, "Guess Windows file format");
+is(Data::Table::fromFileGuessOS("t_mac.csv"), 2, "Guess MAC file format");
+
+my $t_unix=Data::Table::fromFile("t_unix.csv");
+my $t_unix_noheader=Data::Table::fromFile("t_unix_noheader.csv");
+my $t_dos=Data::Table::fromFile("t_dos.csv");
+my $t_mac=Data::Table::fromFile("t_mac.csv");
+
+is_deeply($t_unix->rowRefs, $t_unix_noheader->rowRefs, 'rowRefs t_unix.csv');
+is_deeply($t_unix->rowRefs, $t_dos->rowRefs, 'rowRefs t_dos.csv');
+is_deeply($t_unix->rowRefs, $t_mac->rowRefs, "rowRefs t_mac.csv");
+
diff --git a/t_dos.csv b/t_dos.csv
new file mode 100755
index 0000000..d414f49
--- /dev/null
+++ b/t_dos.csv
@@ -0,0 +1,3 @@
+a,b,c
+1,2,3
+4,5,6
diff --git a/t_dos2.csv b/t_dos2.csv
new file mode 100755
index 0000000..3e72501
--- /dev/null
+++ b/t_dos2.csv
@@ -0,0 +1,3 @@
+a b c
+1 2
+4 6
diff --git a/t_mac.csv b/t_mac.csv
new file mode 100755
index 0000000..57a2cfb
--- /dev/null
+++ b/t_mac.csv
@@ -0,0 +1 @@
+a,b,c
1,2,3
4,5,6
\ No newline at end of file
diff --git a/t_unix.csv b/t_unix.csv
new file mode 100755
index 0000000..88700c7
--- /dev/null
+++ b/t_unix.csv
@@ -0,0 +1,3 @@
+a,b,c
+1,2,3
+4,5,6
diff --git a/t_unix_noheader.csv b/t_unix_noheader.csv
new file mode 100755
index 0000000..da813b6
--- /dev/null
+++ b/t_unix_noheader.csv
@@ -0,0 +1,2 @@
+1,2,3
+4,5,6
diff --git a/ttt.csv b/ttt.csv
new file mode 100755
index 0000000..85f0349
--- /dev/null
+++ b/ttt.csv
@@ -0,0 +1,3 @@
+COL_A,COL_B
+a\tz,b
+p\N,d\tf
diff --git a/ttt.tsv b/ttt.tsv
new file mode 100755
index 0000000..84e9a4e
--- /dev/null
+++ b/ttt.tsv
@@ -0,0 +1,3 @@
+COL_A COL_B
+a\tz b
+p\N d\tf
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libdata-table-perl.git.git
More information about the Pkg-perl-cvs-commits
mailing list