in reply to array of arrays
#!usr/bin/perl use strict; use warnings; use Data::Dumper; my %HoA; # a Hash of Array while (my $line = <DATA>) { my ($bucket, $num) = $line =~ m/^\s*(\d+)\s*\|\s*(\d+)/; push @{$HoA{$bucket}},$num; } my $total; foreach my $key (sort {$a<=>$b} keys %HoA) { my $line_total; foreach my $num (@{$HoA{$key}}) { $line_total += $num; } print "Line $key total = $line_total\n"; $total += $line_total; } print "Grand Total = $total\n"; =Prints Line 1 total = 150 Line 2 total = 75 Line 3 total = 55 Grand Total = 280 =cut __DATA__ 1|10 1|20 1|30 1|40 1|50 2|15 2|25 2|35 3|1 3|2 3|3 3|4 3|5 3|6 3|7 3|8 3|9 3|10
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: array of arrays
by Anonymous Monk on Jun 12, 2017 at 15:23 UTC | |
by Marshall (Canon) on Jun 13, 2017 at 00:45 UTC |