Skip to content

Data::StaticTable

shinobi edited this page Feb 24, 2018 · 20 revisions

Introduction

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.

Types

Data::StaticTable::Position

Basically, an integer greater than 0. Used to indicate a row position in the table. A StaticTable do not have rows on index 0.

Data::StaticTable class

Positional features

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

method new

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

my $t = StaticTable.new(
   (1,2,3),
   (4,5,6),
   (7,8,9)
)

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
):data-has-header

my $t = StaticTable.new(
  @ArrayOfHashes, rejected-data => @rejected
):set-of-hashes

In these cases, you can recover all the data that was discarded

method perl

Shows a summary of the things contained in the StaticTable object. Used for debugging, not for serialization.

method display

Shows the contents of the StaticTable Used for debugging, not for serialization.

method cell(Str $column-header, Position $row)

Retrieves the content of a cell.

method column(Str $column-header)

Retrieves the content of a column like a regular List.

method row(Position $row)

Retrieves the content of a row as a regular List.

method shaped-array()

Retrieves the content of a row as a multiple dimension array.

method elems()

Retrieves the number of cells in the table

method generate-index(Str $heading)

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).

method take(Array[Position] $_rownums)

Generate a new StaticTable, using a list of row numbers (using the type Data::StaticTable::Position)

Clone this wiki locally