http://www.perlmonks.org?node_id=800975

GertMT has asked for the wisdom of the Perl Monks concerning the following question:

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