in reply to How to Extract Data from Text file 1 based on the Value from Text file 2
use warnings and use strict are your friends, you should use them. You have them commented out here. This isn't C where you have # in the front of declarations, in Perl a # at the start of a line is usally for commenting out that line.#use strict; #use warnings;
If you're going to die on failure (which you should do) you'll help yourself a lot by giving as much information as possible as to why it's failed. If the failure is due to an incorrect file name, you'll want to know what file name the program was trying to open, to see if it's different from what you were expecting it to be using. It can help to protect the name in single quotes, like: die "Can't read '$filename': $!\n" so you can easily spot leading or trailing spaces in the name which can catch you. Also use the "$!" at the end of the line which tells you the error message associated with the reason the action failed - this is a BIG help. Here is my quick stab at the code... Things to note, I like to use hash references because it can make it easier to deal with the data later. I've trapped to catch lines with unrecognized customer IDs. I've also sorted by customer name. You could turn these off if not applicable, but they may be useful. I hope this helps...open(CUST_DATA,'<', $cust_file) || die("Could not open file!");
#!/usr/bin/perl # # # use warnings; use strict; my $cust_file = "customer.txt"; my $billing_file = "2010.bill"; 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; @{$bill_line}{@bill_fields} = split(/\s/, $line); if ( ! $cust->{ $bill_line->{cust_id} }->{name} ) { die "$0: Found unknown customer ID [$bill_line->{cust_id}] on +line $. of '$billing_file'\n"; } $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 ) { print "CUSTOMER NAME: $cust->{$customer}->{name}\n" . "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 "$device $short_port $direction " . $bill_data->{$customer}->{$device}->{$port}->{$dir +ection} . $/; } } } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How to Extract Data from Text file 1 based on the Value from Text file 2
by roborat (Novice) on Mar 04, 2010 at 12:30 UTC | |
Re^2: How to Extract Data from Text file 1 based on the Value from Text file 2
by roborat (Novice) on Mar 04, 2010 at 13:24 UTC | |
by serf (Chaplain) on Mar 04, 2010 at 13:49 UTC | |
by roborat (Novice) on Mar 04, 2010 at 15:19 UTC | |
by roborat (Novice) on Mar 05, 2010 at 08:35 UTC | |
by roborat (Novice) on Mar 05, 2010 at 10:47 UTC |