NAME

TableData::Object::Base - Base class for TableData::Object::*

VERSION

This document describes version 0.113 of TableData::Object::Base (from Perl distribution TableData-Object), released on 2021-01-10.

METHODS

new($data[ , $spec]) => obj

Constructor. $spec is optional, a specification hash as described by TableDef.

$td->cols_by_name => hash

Return the columns as a hash with name as keys and index as values.

Example:

{name=>0, gender=>1, age=>2}

$td->cols_by_idx => array

Return the columns as an array where the element will correspond to the column's position.

Example:

["name", "gender", "age"]

$td->row_count() => int

Return the number of rows.

See also: col_count().

$td->col_count() => int

Return the number of columns.

See also: row_count().

$td->col_exists($name_or_idx) => bool

Check whether a column exists. Column can be referred to using its name or index/position (0, 1, ...).

$td->col_content($name_or_idx) => aos

Get the content of a column as an array of strings. Return undef if column is unknown. For example, given this table data:

| name  | age |
|-------+-----|
| andi  | 25  |
| budi  | 29  |
| cinta | 17  |

then $td->col_content('name') or $td->col_content(0) will be:

['andi', 'budi', 'cinta']

$td->col_name($idx) => str

Return the name of column referred to by its index/position. Undef if column is unknown.

See also: col_idx().

$td->col_idx($name) => int

Return the index/position of column referred to by its name. Undef if column is unknown.

See also: col_name().

$td->row($idx) => s/aos/hos

Get a specific row ($idx is 0 to mean first row, 1 for second, ...).

$td->row_as_aos($idx) => aos

Get a specific row ($idx is 0 to mean first row, 1 for second, ...) as aos.

$td->row_as_hos($idx) => hos

Get a specific row ($idx is 0 to mean first row, 1 for second, ...) as hos.

$td->rows() => array

Return rows as array(ref). Each element (row) can either be a scalar (in the case of hash or aos table data) or aos (in the case of aoaos table data) or hos (in the case of aohos table data).

This is appropriate if you only want the rows and do not care about the fom of the row, for example if you want to output some of the rows or shuffle them.

See also: rows_as_aoaos() and rows_as_aohos().

$td->rows_as_aoaos() => aoaos

Return rows as array of array-of-scalars.

See also: rows() and rows_as_aohos().

$td->rows_as_aohos() => aohos

Return rows as array of hash-of-scalars.

See also: rows() and rows_as_aoaos().

$td->select_as_aoaos([ \@cols[ , $func_filter_row[ , \@sorts] ] ]) => aoaos

Like rows_as_aoaos(), but allow selecting columns, filtering rows, sorting.

\@cols is an optional array of column specification to return in the resultset. Currently only column names are allowed. You can mention the same column name more than once.

$func_filter_row is an optional coderef that will be passed ($td, $row_as_hos) and should return true/false depending on whether the row should be included in the resultset. If unspecified, all rows will be returned.

\@sorts is an optional array of column specification for sorting. For each specification, you can use COLUMN_NAME or -COLUMN_NAME (note the dash prefix) to express descending order instead of the default ascending. If unspecified, no sorting will be performed.

See also: select_as_aohos().

$td->select_as_aohos([ \@cols[ , $func_filter_row[ , \@sorts ] ] ]) => aohos

Like select_as_aoaos(), but will return aohos (array of hashes-of-scalars) instead of aoaos (array of arrays-of-scalars).

See also: select_as_aoaos().

$td->uniq_col_names => list

Return a list of names of columns that are unique. A unique column exists in all rows and has a defined and unique value across all rows. Example:

my $td = table([
    {a=>1, b=>2, c=>undef, d=>1},
    {      b=>2, c=>3,     d=>2},
    {a=>1, b=>3, c=>4,     d=>3},
]);
my @colnames = $td->uniq_col_names; # -> ('d')

In the above example, a does not exist in the second hash, <b> is not unique, and c has an undef value in the the first hash.

$td->const_col_names => list

Return a list of names of columns that are constant. A constant column ehas a defined single value for all rows (a column that contains all undef's counts). Example:

my $td = table([
    {a=>1, b=>2, c=>undef, d=>2},
    {      b=>2, c=>undef, d=>2},
    {a=>2, b=>3, c=>undef, d=>2},
]);
my @colnames = $td->const_col_names; # -> ('c', 'd')

In the above example, a does not exist in the second hash, <b> has two different values.

$td->del_col($name_or_idx) => str

Delete a single column. Will die if the underlying form does not support column deletion (e.g. aos and hash).

Will modify data. Will also adjust column positions. And will also modify spec, if spec was given.

Return the deleted column name.

If column is unknown, will simply return undef.

$td->rename_col($old_name_or_idx, $new_name)

Rename a column to a new name. Will die if the underlying form does not support column rename (e.g. aos and hash).

Die if column is unknown. Die if new column name is a number, or an existing column name. Will simply return if new column name is the same as old name.

Might modify data (e.g. in aohos). Will modify spec, if spec was given.

$td->switch_cols($name_or_idx1, $name_or_idx2)

Switch two columns. Will die if the underlying form does not support column rename (e.g. aos and hash).

Die if either column is unknown. Will simply return if both are the same column.

Might modify data (e.g. in aohos). Will modify spec, if spec was given.

$td->add_col($name [ , $idx [ , $spec ] ])

Add a column named $name. If $idx is specified, will set the position of the new column (and existing columns will shift to the right at that position). If $idx is not specified, will put the new column at the end.

Does not make sense for table form which can only have a fixed number of columns, e.g. aos, or hash.

$td->set_col_value($name_or_idx, $value_sub)

Set value of (all rows of) a column. $value_sub is a coderef which will be given hash arguments containing these keys: table (the TableData::Object instance), row_idx (row number, 0-based), col_name (column name), col_idx (column index, 0-based), value (current value).

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/TableData-Object.

SOURCE

Source repository is at https://github.com/perlancar/perl-TableData-Object.

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/perlancar/perl-TableData-Object/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2021, 2020, 2019, 2017, 2016, 2015, 2014 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.