NAME

Data::TableAutoSum - Table that calculates the results of rows and cols automatic

SYNOPSIS

use Data::TableAutoSum;

my $table = Data::TableAutoSum->new(rows => 10, cols => 20); 
# or
my $table = Data::TableAutoSum->new(rows => ['New York', 'L.A.', 'Chicago'],
                                    cols => ['Women', 'Men', 'Alien']);
foreach my $row ($table->rows()) {
   foreach my $col ($table->cols()) {
      $table->data($row,$col) = rand();
      $table->data($row,$col) += $table->data($row-1,$col-1) 
          if $row >= 1 && $col >= 1;
   }
}

print "Row $_ has result: ",$table->rowresult($_) for $table->rows();
print "Col $_ has result: ",$table->colresult($_) for $table->cols();
print "Table has the total result: ",$table->totalresult();

print "Let's have a look to the whole table:\n", $table->as_string;

$table->store('random.dat');
my $old_random_data = Data::TableAutoSum->read('random.dat');

ABSTRACT

Table object with automatic calculation of the row/column sums.

DESCRIPTION

This module represents a table with automatic calculation of the row/column sums.

FUNCTIONS

new(rows => $nr_of_rows || \@rows, cols => $nr_of_cols || \@cols)

Creates a new, zero filled table. You can define the rows or cols with a ref to an array of the names of the rows/cols. If so, the names have to be unique. If you only give a number, the rows/cols are named (0 .. $nr-1).

data($row,$col,$new_value)

Get/set of data elements in the table. $new_value is optional Note, that the return value is an lvalue, so you can e.g. set a new value via $table->data($row,$col) = 4; or modify all values with

foreach my $row ($table->rows) {
  foreach my $col ($table->cols) {
    $table->data($row,$col) *= 1.05;
  }
}
rows(), cols()

These functions are returning all rows/columns in a list. They are returned in the order as given with the new constructor.

It's not possible to set rows/columns with them.

rowresult($row), colresult($col)

Returns the sum for the specified row/col.

I named the methods *result instead of *sum, as I plan to implement a possibility change the operation, e.g. to max or multiplication.

You can't change the results directly. Change the table data for that.

totalresult()

Returns the sum over all data elements. totalresult is equal to the sum of all rowresults or the sum of all colresults (of course, there could be some rounding errors).

You can't change the result directly. Change the table data for that.

as_string()

Returns a string representation of the table. A typical example could be:

      0     1    2   Sum
0     2     9    4    15
1     7     5    3    15
2     6     1    8    15
Sum  15    15   15    45

The string is a multiline string, the elements of the table are seperated with a tab.

store($filename)

Stores the table in a readable format (the same as used by as_string) into the specified file.

store returns the table object itselfs, so you can use it in the fashion way:

print "Stored the table\n", $table->store($filename)->as_string;
Data::TableAutoSum->read($filename)

Constructs a table found in the filename. It expects a table of the format written by store, what is the same like written with as_string.

I didn't test what happens, using wrong formated files or similar. You're supposed to don't do that.

EXPORT

None by default.

BUGS

The store/read methods are slow. As I wrote the module for convienience, it's not so important for me, but I'll change it a day.

It's not tested what happens when you try to read a misformatted table. Don't do that.

If you work with floating point types, don't expect that $table->store('filename')->read('filename') reproduces exactly the same table, as there could be some rounding errors.

I hope there aren't any more bugs.

TODO

options for as_string, store, read

The seperator, the end of line char, and the "Sum"-string should be changeable.

operation

Possibility to change the internal used operation, at the moment, only '+' is used. I'd like to give the possibility to use any other distributive, associative operation.

change

A possibility to make something with all data elements, like

$table->change(sub {$_ *= 2});
merge

A static merging method, that combines two table sets, like

my $population = Data::TableAutoSum::merge(sub {$a + $b},
                                           $female_population,
                                           $male_population);
=item overloaded operators

Some operators should be overloaded. I'd like to write something like

my $population_perc = $population / $population->totalresult;
my $euro_prices     = $dm_prices / 1.95883;

my $population = $female_population + $male_population;
my $murders_per_inhabitant = $murders / $inhabitants;
clear/fill

A clear method, that resets all values to 0 and a fill method to fill all elements with a specific value.

subtables

Something like

my $east_alien_population = $population->subtable(rows => ['Chicago', 'New York'],
                                                  cols => 'alien');

Quite an insert_subtable method seems sensful, too.

increase speed of store/read

REQUIREMENTS

Params::Validate
Regexp::Common
Set::Scalar
List::Util 
Tie::File
    
Math::Random            # for the tests
Set::CrossProduct  
Data::Dumper       

Test::More             
Test::Exception    
Test::Builder

SEE ALSO

Data::Xtab, Data::Pivot, Table::Pivoter

AUTHOR

Janek Schleicher, <bigj@kamelfreund.de>

COPYRIGHT AND LICENSE

Copyright 2002 by Janek Schleicher

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