The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

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

VERSION

This document describes version 0.116 of Data::TableData::Object::Base (from Perl distribution Data-TableData-Object), released on 2022-05-19.

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},
 ]); # -> ('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},
 ]); # -> ('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 [ , \@data ] ] ])

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.

@data is the value of the new column for each row. If not specified, the new column will be set to undef.

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 Data::TableData::Object instance), row_idx (row number, 0-based), col_name (column name), col_idx (column index, 0-based), value (current value).

$td->iter

Generate a simple (coderef) iterator to iterate rows of the table. Will call row() repeatedly from the first row (index 0) to the last (determined by $td->row_count). After the rows are exhausted, the iterator will return undef. Note that for an aos table, it is possible that a row has the value of undef and thus the iterator can return undef before the rows are exhausted.

HOMEPAGE

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

SOURCE

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

AUTHOR

perlancar <perlancar@cpan.org>

CONTRIBUTING

To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required beyond that are considered a bug and can be reported to me.

COPYRIGHT AND LICENSE

This software is copyright (c) 2022, 2021, 2019, 2017, 2016, 2015, 2014 by perlancar <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.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Data-TableData-Object

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.