I'm doing an exercise from "Intermediate Perl" -- anyway, in the below code, the YAML Dump comes out correct but the foreach (marked HERE) yields just a single array reference (ARRAY), as if tmp only had one element, although it is the exact same array.
#!/usr/bin/perl -w
use strict;
use YAML::XS;
my %all;
open(INPUT,"<coconet.dat") || die "Can't open coconet.dat";
while (<INPUT>) {
if ($_ =~ /^#/) { next }
my ($src, $dest, $bytes) = split;
$all{$src}{$dest}+=$bytes;
}
close(INPUT);
my @reflist = sort { ${$$b}[2] <=> ${$$a}[2] } map \&buildlist($_), ke
+ys %all;
foreach my $ref (@reflist) {
print "${$$ref}[0] ${$$ref}[2]\n";
my @tmp=@{$$ref}[3];
#HERE foreach my $line (@tmp) { print "\t$line\n" }
print Dump @tmp;
}
sub buildlist {
my $site = $_;
my @tmp;
$tmp[0]=$site;
$tmp[1]=\%{$all{$site}};
foreach (keys %{$tmp[1]}) { $tmp[2]+=${$tmp[1]}{$_} }
@{$tmp[3]} = sort { ${$tmp[1]}{$b} <=> ${$tmp[1]}{$a} } keys %{$tm
+p[1]};
return \@tmp; # 0=site name, 1=hash ref, 2=total bytes, 3=sorted
+ dest refs
}
Why would there be this difference and what's wrong with my foreach??
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.