What you called "CABABILITIES", I called "roles" for short.
That which has "CABABILITIES", I called "actor".

Instead of building a hash of roles by actor, build a hash of actors by role. That way, your data structure will ressemble your output. (Update: Oops, that is what you are doing. The rest of the post applies, though.)

In the code below, I replaced doing the same thing to three different arrays with a single loop.

use strict; use warnings; my @arrays = ( [ qw( A 1 2 3 4 5 ) ], [ qw( B 1 2 5 6 ) ], [ qw( C 2 3 ) ], ); my %actors_by_role; foreach my $array (@arrays) { my ($actor, @roles) = @$array; foreach my $role (@roles) { push @{$actors_by_role{$role}}, $actor; } } my @roles = sort keys %actors_by_role; foreach my $role (@roles) { print(join("\t", $role => sort @{$actors_by_role{$role}}), "\n"); }

Output:

1 A B 2 A B C 3 A C 4 A 5 A B 6 B

From there, it's not hard to get the second output.

use strict; use warnings; my @arrays = ( [ qw( A 1 2 3 4 5 ) ], [ qw( B 1 2 5 6 ) ], [ qw( C 2 3 ) ], ); my %actors; my %actors_by_role; foreach my $array (@arrays) { my @roles = @$array; my $actor = shift(@roles); $actors{$actor} = 1; foreach my $role (@roles) { $actors_by_role{$role}{$actor} = 1; } } my @roles = sort keys %actors_by_role; my @actors = sort keys %actors; { foreach my $actor (@actors) { print("\t", $actor); } print("\n"); } foreach my $role (@roles) { print($role); foreach my $actor (@actors) { print("\t", $actors_by_role{$role}{$actor} ? 'X' : ' '); } print("\n"); }

Output:

A B C 1 X X 2 X X X 3 X X 4 X 5 X X 6 X

In reply to Re: Multiple arrays in a hash by ikegami
in thread Multiple arrays in a hash by gasho

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.