Text::Table::Simple

Create basic tables from a two dimensional array

Text::Table::Simple

Create basic tables from a two dimensional array

Synopsis

use Text::Table::Simple;

my @columns = <id name email>;
my @rows    = (
    [1,"John Doe",'[email protected]'],
    [2,'Jane Doe','[email protected]'],
);

my @table = lol2table(@columns,@rows);
.say for @table;

# O----O----------O-------------------------O
# | id | name     | email                   |
# O====O==========O=========================O
# | 1  | John Doe | [email protected]        |
# | 2  | Jane Doe | [email protected] |
# -------------------------------------------

Exports

lol2table(@body, *%options --> Str @rows)

lol2table(@header, @body, @footer?, *%options --> Str @rows)

Create a an array of strings that can be printed line by line to create a table view of the data.

> my @cols = <XXX XXXX>;
> my @rows = ([1,2],[3,4]);
> say lol2table(@cols, @rows).join("\n");

O-----O------O
| XXX | XXXX |
O=====O======O
| 1   | 2    |
| 3   | 4    |
--------------
Options
# default values
%options = %(
    rows => {
        column_separator     => '|',
        corner_marker        => '-',
        bottom_border        => '-',
    },
    headers => {
        top_border           => '-',
        column_separator     => '|',
        corner_marker        => 'O',
        bottom_border        => '=',
    },
    footers => {
        column_separator     => 'I',
        corner_marker        => '%',
        bottom_border        => '*',
    },
);

You can replace any of the default options by passing in a replacement. C<corner_marker> is used when more specific corner markers are not set.

> my %options =
    rows => {
        column_separator           => 'ā”‚',
        bottom_left_corner_marker  => 'ā””',
        bottom_right_corner_marker => 'ā”˜',
        bottom_corner_marker       => 'ā”“',
        bottom_border              => 'ā”€',
    },
    headers => {
        top_border                 => 'ā•',
        column_separator           => 'ā”‚',
        top_corner_marker          => 'ā•¤',
        top_left_corner_marker     => 'ā•’',
        top_right_corner_marker    => 'ā••',
        bottom_left_corner_marker  => 'ā•ž',
        bottom_right_corner_marker => 'ā•”',
        bottom_corner_marker       => 'ā•Ŗ',
        bottom_border              => 'ā•',
    };
> my @columns = <id name email>;
> my @rows    = (
    [1,"John Doe",'[email protected]'],
    [2,'Jane Doe','[email protected]'],
);

> .put for lol2table(@columns, @rows, |%options);

ā•’ā•ā•ā•ā•ā•¤ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•¤ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā••
ā”‚ id ā”‚ name     ā”‚ email                   ā”‚
ā•žā•ā•ā•ā•ā•Ŗā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•Ŗā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•”
ā”‚ 1  ā”‚ John Doe ā”‚ [email protected]        ā”‚
ā”‚ 2  ā”‚ Jane Doe ā”‚ [email protected] ā”‚
ā””ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Examples

Showing your Benchmark output:

use Text::Table::Simple;

use Text::Levenshtein::Damerau;
use Benchmark;

my $str1 = "lsd";
my $str2 = "lds";

my %results = timethese(1000, {
    'dld' => sub { Text::Levenshtein::Damerau::{"&dld($str1,$str2)"} },
    'ld ' => sub { Text::Levenshtein::Damerau::{"&ld($str1,$str2)"}  },
});

my @headers = ['func','start','end','diff','avg'];
my @rows    = %results.map: {.key, .value.Slip}
my @table   = lol2table(@headers,@rows);

.say for @table;

Also see the examples directory.

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.