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

Hi Perl Monks, I need help to get the total number of my customer data. Here's the current script I am using:

#!/usr/bin/perl # # use warnings; use strict; my $cust_file = "customer.txt"; my $billing_file = "20100301.bill"; my $lastmonth_period = "1Feb2010 - 28Feb2010"; my $cust; open (CUST_DATA, $cust_file) || die "Can't read '$cust_file': $!\n"; while (defined (my $line = <CUST_DATA>)) { chomp $line; # Remove trailing newline my ($cust_id,$cust_name) = split(/:/,$line); $cust->{$cust_id}->{name} = $cust_name; } close(CUST_DATA); my @bill_fields = qw(cust_id x_cust_id2 Device_Code Port Traf_Dir x_st +art_date x_end_date x_number Data_Usage); my $bill_line; my $bill_data; open(BILL_DATA, $billing_file) || die "Can't read '$billing_file': $!\ +n"; while (defined (my $line = <BILL_DATA>)) { chomp $line; # Remove trailing newline @{$bill_line}{@bill_fields} = split(/\s/, $line); $bill_data->{ $bill_line->{cust_id} }->{ $bill_line->{Device_Code} + }->{ $bill_line->{Port} }->{ $bill_line->{Traf_Dir} } = $bill_line-> +{Data_Usage}; } close(BILL_DATA); sub by_name ($$) { return $cust->{$a}->{name} <=> $cust->{$b}->{name} +}; for my $customer ( keys %$cust ) { # Write to a file named with the customer name my $output = "./$cust->{$customer}->{name}.txt"; open (CUST_OUT, ">$output") || die "Can't write to '$output': $!\n +"; print CUST_OUT "Data Transfer Report for $cust->{$customer}->{name +}\n" . "Period Device_Code Port T +raf_Dir Data_Usage\n"; for my $device ( sort by_name keys %{$bill_data->{$customer}} ) { for my $port ( sort keys %{$bill_data->{$customer}->{$device}} + ) { (my $short_port = $port) =~ s/^FastEthernet/Fa/g; for my $direction qw(OUT IN) { print CUST_OUT "$lastmonth_period $device $short_port +$direction" . $bill_data ->{$customer}->{$device}->{$port}->{$di +rection} . $/; } } } close(CUST_OUT); }


Current output:

Data Transfer Report for ABC Period Device_Code Port Traf_Dir Data_Usage
1Feb2010 - 28Feb2010 SWITCH Fa1_0_1 OUT129.641
1Feb2010 - 28Feb2010 SWITCH Fa1_0_1 IN50.947
1Feb2010 - 28Feb2010 SWITCH Fa1_0_2 OUT19.723
1Feb2010 - 28Feb2010 SWITCH Fa1_0_2 IN11.478

I need to compute the total Data Usage such as:

Data Transfer Report for ABC Period Device_Code Port Traf_Dir Data_Usage
1Feb2010 - 28Feb2010 SWITCH Fa1_0_1 OUT129.641
1Feb2010 - 28Feb2010 SWITCH Fa1_0_1 IN50.947
1Feb2010 - 28Feb2010 SWITCH Fa1_0_2 OUT19.723
1Feb2010 - 28Feb2010 SWITCH Fa1_0_2 IN11.478

Total Data_Usage: 211.789 ( Need help to get this value... )

Thanks in advance for your help.

Replies are listed 'Best First'.
Re: Calculate the Sum of each customer data
by almut (Canon) on Mar 07, 2010 at 13:02 UTC

    Just in case... :)

    for my $customer ( keys %$cust ) { my $total_usage = 0; ... ... for my $direction qw(OUT IN) { my $usage = $bill_data ->{$customer}->{$device}->{$por +t}->{$direction}; $total_usage += $usage; print CUST_OUT "$lastmonth_period $device $short_port +$direction$usage$/"; } ... print out $total_usage close(CUST_OUT); }
      Hi Almut, thanks a lot for your help! This is exactly what I want. :)
Re: Calculate the Sum of each customer data
by Corion (Patriarch) on Mar 07, 2010 at 12:43 UTC

    How about you sum up the "data usage" per customer and then output the total? I'm not sure where your problem is.

Re: Calculate the Sum of each customer data
by roborat (Novice) on Mar 07, 2010 at 12:31 UTC
    Oops.. I forgot to put the two files used.

    Customer.txt

    200207:ABC
    200905:DEF
    200403:GHI
    200010:JKL


    20100310.bill

    200207 200207 SWITCH FastEthernet1_0_1 OUT 01-Feb-2010_02:00 01-Mar-2010_02:00 428.706 129.641
    200207 200207 SWITCH FastEthernet1_0_1 IN 01-Feb-2010_02:00 01-Mar-2010_02:00 168.476 50.947
    200207 200207 SWITCH FastEthernet1_0_2 OUT 01-Feb-2010_02:00 01-Mar-2010_02:00 65.223 19.723
    200207 200207 SWITCH FastEthernet1_0_2 IN 01-Feb-2010_02:00 01-Mar-2010_02:00 37.958 11.478
    200905 200905 SWITCH FastEthernet1_0_4 OUT 01-Feb-2010_02:00 01-Mar-2010_02:00 3.193 0.965
    200905 200905 SWITCH FastEthernet1_0_4 IN 01-Feb-2010_02:00 01-Mar-2010_02:00 41.499 12.549
    200403 200403 SWITCH FastEthernet1_0_5 OUT 01-Feb-2010_02:00 01-Mar-2010_02:00 1.396 0.422
    200403 200403 SWITCH FastEthernet1_0_5 IN 01-Feb-2010_02:00 01-Mar-2010_02:00 38.862 11.752
    200010 200010 SWITCH FastEthernet1_0_6 OUT 01-Feb-2010_02:00 01-Mar-2010_02:00 3.386 1.024
    200010 200010 SWITCH FastEthernet1_0_6 IN 01-Feb-2010_02:00 01-Mar-2010_02:00 4.489 1.358