for my $customer ( keys %$cust ) { # If you want to write to a file named with the customer ID # my $output = "./$customer.out"; # If you want to 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 Traf_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}->{$direction} . $/; } } } close(CUST_OUT); }