-
Notifications
You must be signed in to change notification settings - Fork 3
Data::StaticTable
A StaticTable allows you to handle bidimensional data in a more natural way
Some features:
- Rows starts at 1 (
Data::StaticTable::Position
is the datatype used to reference row numbers) - Columns have header names
- Any column can work as an index
If the number of elements provided does not suffice to form a square or a rectangle, empty cells will be added.
The module provides two classes: StaticTable
and StaticTable::Query
.
A StaticTable can be populated, but it can not be modified later. To perform searchs and create/store indexes, a Query object is provided. You can add indexes per column, and perform searches (grep) later. If an index exists, it will be used.
You can get data by rows, columns, and create subsets by taking some rows from an existing StaticTable.
Basically, an integer greater than 0. Used to indicate a row position in the table. A StaticTable do not have rows on index 0.
You can use [n] to get the full Nth row, in the way of a hash of 'Column name' => data
So, for example
$Q1[1]
Could return a hash like
{Column1 => 10, Column2 => 200.4, Column3 => 450}
And a call like
$Q1[10]<Column3>
would refer to the data in Row 10, with the heading Column3
my $t = StaticTable.new( 3 , (1 .. 15) );
my $t = StaticTable.new(
<Column1 Column2 Column3> ,
(
1, 2, 3,
4, 5, 6,
7, 8, 9,
10,11,12
13,14,15
)
);
Create a StaticTable, by specifying a header (one by one or just by numbers). If you a number, this number of columns will be used, and automatically named as A, B, C ... Z, AA, AB, ...
This will create a spreadsheet-like table, with numbered rows and labeled columns.
If you do not provide enough data to fill the last row, empty cells will be appended.
You can also create a StaticTable from an array, with each element representing a row. The StaticTable will acommodate the values as best as possible, adding empty values or discarding values that go beyond the boundaries, or data that is not prepared appropiately
This constructor can be called like this, using an Array of Arrays
my $t = StaticTable.new(
(1,2,3),
(4,5,6),
(7,8,9)
);
For a Array of Hashes, you can call it like this
my $t = StaticTable.new(
[
{ name => 'Eggplant', color => 'aubergine', type => 'vegetal' },
{ name => 'Egg', color => ('white', 'beige'), type => 'animal' },
{ name => 'Banana', color => 'yellow', type => 'fruit' },
{ name => 'Avocado', color => 'green', type => 'fruit', class => 'Hass' }
]
);
(Note the use of brackets, we needed to be explicitly pass an Array of Hashes)
There is a set of named parameters usable in this constructor
my $t = StaticTable.new(@ArrayOfArrays):data-has-header
This will use the first row as header. Any value that falls outside the column boudaries will be discarded.
my $t = StaticTable.new(@ArrayOfHashes):set-of-hashes
This will consider each row as a hash, and will create columns for each key found. The most populated will be the first columns. Any row that is not a hash will be discarded
my $t = StaticTable.new(
@ArrayOfArrays, rejected-data => %rejected # <--- Please note, rejected is a hash
):data-has-header
my $t = StaticTable.new(
@ArrayOfHashes, rejected-data => @rejected # <--- In this case, rejected is an array
):set-of-hashes
In these cases, you can recover all the data that was discarded
Shows a summary of the things contained in the StaticTable object. Used for debugging, not for serialization.
Shows the contents of the StaticTable Used for debugging, not for serialization.
Retrieves the content of a cell.
Retrieves the content of a column like a regular List
.
Retrieves the content of a row as a regular List
.
Retrieves the content of a row as a multiple dimension array.
Retrieves the number of cells in the table
Generate a Hash
, where the key is the value of the cell, and the values is a list of row numbers (of type Data::StaticTable::Position
).
Generate a new StaticTable
, using a list of row numbers (using the type Data::StaticTable::Position
)