Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found