slothrop has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone,

I am currently writing a script to report on problematic entries in our company project management software's address list. I want to emit a a report in plain text, using Perl6::Form if possible. So far, it is mostly working, except for reporting entries with duplicate email addresses. While traversing the database, I gather duplicate email addresses in to a hash of lists, where the keys are the email adresses and the respective lists contain the names of the people who have that address listed.

I would like the output to look something like this:
+------------------------------------------------------------+
| fake_address1@example.com              John Doe            |
|                                        Jane Doe            |
|                                                            |
| fake_address2@example.com              John Doe            |
|                                        Jane Doe            |
...
Currently, I am calling form like this:
my %table = get_duplicates(); # More complicated, but you get the idea +. my @addresses = sort keys %table; my $report = form {bullet => '*'}, '+-----------------------------------------------------------+', '| {[[[[[[[[[[[[[[[[[[[[[[[[[[[} * {[[[[[[[[[[[[[[[[[[[[[} |', \@addresses, [map { $table{$_} @adresses], '| |', '+-----------------------------------------------------------+';

When I my script, however, the output looks more like this:

+-------------------------------------------------------+
| fake_address1@example.com           ARRAY(0x12345678) |
|                                                       |
| fake_address2@example.com           ARRAY(0x90abcdef) |
...
I do get what is happening here, but is there a way for Perl6::Form to format my data the way I want it to?
I could use a full-blown templating library instead of Perl6::Form, but I would really prefer not to.
Is my approach entirely mistaken or am I just missing a minor detail?
Thank you very much for any insights you might feel like sharing with me!

Replies are listed 'Best First'.
Re: Formatting nested data structure in Perl6::Form
by poj (Abbot) on Aug 14, 2014 at 14:37 UTC
    #!perl use strict; use Perl6::Form; my %table = ( name1 => ['b1','c1','d1'], name2 => ['b2','c2'], name3 => ['a3'], name4 => ['c4','d4','e4'], ); my @address; my @name; for my $adr (sort keys %table){ push @address,$adr; push @name, join "\r", @{$table{$adr}},' '; } my $report = form { layout=>"tabular"}, '+-----------------------------------------------------------+', '| {[[[[[[[[[[[[[[[[[[[[[[[[[[[} {[[[[[[[[[[[[[[[[[[[[[} |', \@address, \@name, '| |', '+-----------------------------------------------------------+'; print $report;
    poj
Re: Formatting nested data structure in Perl6::Form
by kennethk (Abbot) on Aug 14, 2014 at 15:18 UTC
    I'm guessing your issue is that %table is a hash of arrays. I would propose perhaps the best solution is a join (untested):
    my $report = form {bullet => '*'}, '+--------------------------------- +--------------------------+', '| {[[[[[[[[[[[[[[[[[[[[[[[[[[[} * {[[[ +[[[[[[[[[[[[[[[[[[} |', \@addresses, [map {join ", " @{$table{$_}} @a +dresses], '| |', '+-------------------------------------------------- +---------+';

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Formatting nested data structure in Perl6::Form
by slothrop (Initiate) on Aug 15, 2014 at 11:27 UTC

    Thank you very, very much! Alas, I had to resort to basically formatting each email_address => names pair individually and joining them.

    I had hoped that Perl6::Form would support my scenario, but now I have a solution that works. It is probably less efficient than it could be, but the number of records I have to deal with is low enough (a few hundred email addresses with about 2.5 people per address on average) that it does not really matter. The script takes a few seconds to run on my laptop, which is entirely acceptable given that this script will run about once a week.

    Again, thank you very much for your help!