The following is a quick hack that generates test data, processes it, and displays it in a grid form. The function table() is adapted from a post I made a day or two ago.
use strict;
use warnings;
my (%pairs, %animals, %owns, @names, @animals, @owns, @display, $name,
+ $animal, $x, $y);
@names = qw/Adrian Alannah Allison Angus Anna Ashton Aurelia Autumn/;
@animals = qw/cockatoo crane crow dove duck egret emu flamingo goose/;
for (1..($#names * $#animals / 2)) {
$name = $names[int rand($#names + 1)];
$animal = $animals[int rand($#animals + 1)];
if (!$owns{$name}{$animal}) {
$owns{$name}{$animal} = 1;
push @owns, [$name, $animal];
}
}
@owns = sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @owns;
print join "\n", map { "$_->[0] owns $_->[1]" } @owns;
print "\n\n";
for (values %owns) {
@owns = keys %$_;
$animals{$_} = 1 for @owns;
for $x (0..($#owns-1)) {
for $y (($x+1)..$#owns) {
$pairs{"$owns[$x] $owns[$y]"}++;
$pairs{"$owns[$y] $owns[$x]"}++;
}
}
}
@animals = sort keys %animals;
push @display, ['', @animals];
for $y (@animals) {
push @display, [$y, map { $pairs{"$y $_"} ? $pairs{"$y $_"} : '' }
+ @animals];
}
table(' | ', \@display);
sub table {
my ($separator, $arr) = @_;
my ($i, @lengths, $length, $format);
for (@$arr) {
for $i (0..$#$_) {
$lengths[$i] = length($_->[$i]) if !$lengths[$i] || $lengt
+hs[$i] < length($_->[$i]);
}
}
$length = 0;
$length += $_ for @lengths;
$length += length($separator) * $#lengths + 4;
$format = join $separator, map { '%-'.$_.'s' } @lengths;
print '-' x $length, "\n";
no warnings;
print '| ', sprintf($format, @$_), ' |', "\n",
'-' x $length, "\n" for @$arr;
}
|