I think you misread the O.P. This isn't about comparing lists, and your code snippet can't get there.

Maybe this will do:

use strict; use warnings; my @id1 = (1..5); my @id2 = (11..15); my %hash; my %col_header; my %row_header; my $last = $#id1 > $#id2 ? $#id1 : $#id2; for my $i (0..$last) { if ( defined( $id2[$i] ) ) { $col_header{$id2[$i]} = 1; } if ( defined( $id1[$i] ) ) { $row_header{$id1[$i]} = 1; } if ( defined( $id1[$i] ) and defined( $id2[$i] ) ) { $hash{$id1[$i]}{$id2[$i]} = 1; } } my @col_header = sort numeric keys %col_header; my @row_header = sort numeric keys %row_header; printf "%10s" x (@col_header + 1), "id1\\id2", @col_header; print "\n"; for my $row ( @row_header ) { printf "%10s", "$row"; for my $col ( @col_header ) { printf "%10s", ( exists( $hash{$row}{$col} ) ? "1" : "0"); } print "\n"; } sub numeric { $a <=> $b; }

with output:

id1\id2 11 12 13 14 15 1 1 0 0 0 0 2 0 1 0 0 0 3 0 0 1 0 0 4 0 0 0 1 0 5 0 0 0 0 1

(I'll leave it to OMAR to turn this into a sub.)

Update: here's the read-from-STDIN version:

use strict; use warnings; my $pair_name; while (<>) { $pair_name = join '\\', split; last; } my %hash; my %col_header; my %row_header; while (<>) { my @pair = split; $hash{$pair[0]}{$pair[1]} = $row_header{$pair[0]} = $col_header{$pair[1]} = 1; } my @col_header = sort numeric keys %col_header; my @row_header = sort numeric keys %row_header; printf "%10s" x (@col_header + 1), $pair_name, @col_header; print "\n"; for my $row ( @row_header ) { printf "%10s", "$row"; for my $col ( @col_header ) { printf "%10s", ( exists( $hash{$row}{$col} ) ? "1" : "0"); } print "\n"; } sub numeric { $a <=> $b; }

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re^2: how to convert two lists into adjacency matrix by QM
in thread how to convert two lists into adjacency matrix by koleti

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.