in reply to Grouping of 2D arrays
See use, strict, warnings.
See perlsyn, perlre, my, push, grep, printf, sprintf.
use strict; use warnings; my (%items,@list); while(<DATA>) { my @l = $_ =~/(\d+)\s([\w\s]+?)\s+([\d,]+)/; push @list,$l[1]; $items{$l[1]}->{count} += $1; # bug? $l[2] =~s/,/./; $items{$l[1]}->{sum} += $l[2]; } my %s; for (grep {! $s{$_}++} @list) { $items{$_}->{sum} =~ s/\./,/; printf "%d % -9s %s\n", $items{$_}->{count},$_,$items{$_}->{sum}; } __DATA__ 1 PC BOX 20,0 1 PC Spare 32,0 1 PC Spare 5,2 1 PE Ak 100,0
This should get you started. Don't use this as is, understand and improve it. Add code to test whether the array @l is really populated (i.e. the match succeeds), for instance.
Convert your $ to cents and back, because you don't want floating point operations dealing which currency.
Why the is the "bug" comment in there?
|
|---|