roborat has asked for the wisdom of the Perl Monks concerning the following question:
#!/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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Calculate the Sum of each customer data
by almut (Canon) on Mar 07, 2010 at 13:02 UTC | |
by roborat (Novice) on Mar 07, 2010 at 13:34 UTC | |
|
Re: Calculate the Sum of each customer data
by Corion (Patriarch) on Mar 07, 2010 at 12:43 UTC | |
|
Re: Calculate the Sum of each customer data
by roborat (Novice) on Mar 07, 2010 at 12:31 UTC |