Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi roborat, welcome to Perl Monks. While almut was away coding you a solution, I was doing the same... and got a few interruptions so was beaten to it... There are a couple of things that I would correct in your example code, because they will help you in your future work.
#use strict; #use warnings;
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.
open(CUST_DATA,'<', $cust_file) || die("Could not open file!");
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...
#!/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} . $/; } } } }

In reply to Re: How to Extract Data from Text file 1 based on the Value from Text file 2 by serf
in thread How to Extract Data from Text file 1 based on the Value from Text file 2 by roborat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found