Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The article Unix Review Column 15 (July 1997) http://www.stonehenge.com/merlyn/UnixReview/col15.html gave me some good excercise to play around with using a hash. Until recently I relied heavily on a module to create a matrix from a multi-dimensional hash. With this article I get some idea how to achieve this without a module.

While trying to create a total-row (bytes-from) at the end and total-column (bytes-to) at the bottom I found a solution for the first but got myself in trouble with the second.

To my idea the structure as it is right now doesn't make this possible? How to make sure the right following order is achieved? Can somebody explain to me what went wrong. Maybe I should set this up in a completely different way?

Thanks, Gert

Below the code from the article (that had a script to generate sample data) and my attempt to create total-row/column

#!/usr/bin/perl use strict; use warnings; use diagnostics; my ( $from, $to, $hh, $mm, $bytes ) = (); my %from_to_bytes = (); while (<DATA>) { ( $from, $to, $hh, $mm, $bytes ) = /^(\S+) (\S+) (\d+):(\d+) (\d+) +$/ or ( warn "bad format on line $.: $_" ), next; $from_to_bytes{$from}{$to} += $bytes; } my %to_hosts = (); for my $from ( sort keys %from_to_bytes ) { my $second = $from_to_bytes{$from}; my @keys = keys %$second; @to_hosts{@keys} = (); } my @to_hosts = sort keys %to_hosts; printf "%10s:", "bytes to"; for (@to_hosts) { printf " %10s", $_; } print "\n"; my $total = (); for my $from ( sort keys %from_to_bytes ) { printf "%10s:", $from; for my $to (@to_hosts) { my $bytes = $from_to_bytes{$from}{$to} || "- none -"; printf " %10s", $bytes; { no warnings; $total += $bytes; } # Calculate total row } printf " %5s", "$total"; $total = 0; # otherwise totalrow acc +umulates print "\n"; } for my $to (sort keys %from_to_bytes){ printf "%10s", "$from_to_bytes{$to}"; } __DATA__ barney barney 08:24 91 fred wilma 05:51 92 fred fred 11:20 83 fred wilma 11:07 76 wilma fred 22:07 3 barney barney 13:22 93 fred fred 06:52 95 fred wilma 17:21 92 barney barney 07:35 95 barney fred 09:58 14 betty barney 12:34 59 wilma barney 09:59 18 betty betty 22:44 29 wilma betty 07:39 99 wilma fred 14:30 91 betty barney 01:54 95 wilma fred 15:45 2

In reply to matrix - creating total row/column by GertMT

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 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? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2021-10-21 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (83 votes). Check out past polls.

    Notices?