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; }

In reply to Re: help with 2D arrays with perl requested by TedPride
in thread help with 2D arrays with perl requested by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.