use strict; use warnings; # Place to accumulate the values my %sum; # Place to store data before we print it. When the KEY field changes, we can then # print all this data to the output my @data; my $previous_KEY=""; while (my $INPUT_LINE = ) { chomp $INPUT_LINE; # Fetch out the values for the current line my ($EquipName, $slot, $port, $PvcCount, $LCir, $RCir) = split /\s+/, $INPUT_LINE; # Create the key to add the data to the proper thing my $KEY = $EquipName . "." . $slot . "." . $port; # If KEY doesn't match the old KEY, print the data then delete # the data we no longer need. if ($previous_KEY ne $KEY) { print_data(); # then discard the (now unneeded) data and totals @data = (); %sum = (); $previous_KEY = $KEY; } # Add the values to the accumulators $sum{PvcCount} += $PvcCount; $sum{LCir} += $LCir; $sum{RCir} += $RCir; # Store the data so we can print it later push @data, [ $., $PvcCount, $LCir, $RCir ]; } # Print data that may still be in @data and %sums print_data(); # We made a subroutine to print the data because we need to # call it from different places. Inside the loop, we need to # print the data when the key changes. But we also need to # call this it the end because there will probably still be # some data in @data and %sum. sub print_data { my $KEY = shift; for my $row (@data) { print "Line $row->[0]: (PrvCount, LCir, RCir) " . "CUR:($row->[1], $row->[2], $row->[3]) " . "TTL:($sum{PvcCount}, $sum{LCir}, $sum{RCir})\n"; } } __DATA__ Switch 1 FF 1 2 3 Switch 1 FF 2 4 9 Switch 1 FF 3 8 27 Switch 1 FF 4 16 81 Router 5 DA 8 5 1 Router 5 DA 9 6 3 Router 5 DA 7 1 9 $ perl pm_11112793.pl Line 1: (PrvCount, LCir, RCir) CUR:(1, 2, 3) TTL:(10, 30, 120) Line 2: (PrvCount, LCir, RCir) CUR:(2, 4, 9) TTL:(10, 30, 120) Line 3: (PrvCount, LCir, RCir) CUR:(3, 8, 27) TTL:(10, 30, 120) Line 4: (PrvCount, LCir, RCir) CUR:(4, 16, 81) TTL:(10, 30, 120) Line 5: (PrvCount, LCir, RCir) CUR:(8, 5, 1) TTL:(24, 12, 13) Line 6: (PrvCount, LCir, RCir) CUR:(9, 6, 3) TTL:(24, 12, 13) Line 7: (PrvCount, LCir, RCir) CUR:(7, 1, 9) TTL:(24, 12, 13)