Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Try the code below. The changes include
  • adding chomp to get rid of the extra newlines that are left on the $val
  • removing the spaces, along with the commas, on the split, so they don't end up as part of the hash indices
  • using a canonical form for the table index, so that "ob1,bo2" and "ob2,ob1" go in the same bin.
  • just one hash for which rows and columns are present
  • print "-" if there's no entry

You were well on your way, but the first two on this list can confuse things enough that you can't see the rest. That's the way it happens, it's the things you're not looking at that get you.

use strict; my %table; my %rows_cols; my %cols; for(<DATA>) { chomp; my($row,$col,$val) = split ', *'; if ($row>$col) { ($row,$col) = ($col,$row); } $table{$row}{$col} = $val; $rows_cols{$row}++; $rows_cols{$col}++; } for my $col (sort keys %rows_cols) { print "\t$col"; } print "\n"; my $val; for my $row (sort keys %rows_cols) { print "$row\t"; for my $col (sort keys %rows_cols) { if (defined $table{$row}{$col}) { print $table{$row}{$col}; } elsif (defined $table{$col}{$row}) { print $table{$col}{$row}; } else { print "-"; } print "\t"; } print "\n"; } __END__ ob1, ob2, 34 ob1, ob3, 56 ob1, ob4, 12 ob1, ob5, 78 ob1, ob6, 23 ob3, ob1, 56 ob7, ob1, 23 ob8, ob1, 12 ob9, ob1, 90 ob3, ob2, 87
prints
ob1 ob2 ob3 ob4 ob5 ob6 ob7 ob8 + ob9 ob1 - 34 56 12 78 23 23 12 + 90 ob2 34 - 87 - - - - - + - ob3 56 87 - - - - - - + - ob4 12 - - - - - - - + - ob5 78 - - - - - - - + - ob6 23 - - - - - - - + - ob7 23 - - - - - - - + - ob8 12 - - - - - - - + - ob9 90 - - - - - - - + -

In reply to Re: printing out a matrix for a data list by rodion
in thread printing out a matrix for a data list by Angharad

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 rifling through the Monastery: (3)
As of 2024-03-30 07:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found