Hello All,
I hope you all are doing well.
I am trying to get this code to work correctly and I have used another Perl Guru's
code [Thanks Charles Clarkson] to get this far, but I am still unsure about a few
things...
I am trying to read a file (attached). and I am trying to make a csv file of it.
Here is my code.
#!/perl -w
use strict;
use warnings;
use Diagnostics;
use Data::Dumper 'Dumper';
#find the date report you need
#open the report
open(FILE,"SAM65 financial report.txt") or die("Can't open report");
my %report;
my %rpt_title;
my %record;
print csv( {
name => 'name',
count => 'count',
total => 'total',
ext_total => 'ext_total'
} ), "\n";
while(<FILE>){
if($.>3){
print csv( \%record ), "\n" if is_valid_ad( \%record );
# Set field defaults
%record = (
name => '',
count => '',
total => '',
ext_total => ''
);
# update %record for this line
@record{ qw| name count total ext_total | }
= split(/\t/, $_);
}#end print csv
}#end if.. to skip first three lines.
print csv( \%record ), "\n" if is_valid_ad( \%record );
sub is_valid_ad {
my $field = shift;
foreach my $value ( values %$field ) {
# return true if one field is valid
return 1 unless $value eq '';
}
# return false if we get here
return;
}
sub csv {
my $field = shift;
# if you need to change field order
# do it here
my @field_order = qw|name count total ext_total|;
# add double quotes (")
foreach my $value ( values %$field ) {
$value = "NULL" if(!defined($value)); #if there is no match, then make that
field null
$value =~ s/^\s+//;#remove leading whitespace
$value =~ s/\s+$//;#remove trailing whitespace
$value = qq("$value");
}
# use a hash slice to order the fields
return join ',', @$field{ @field_order };
}
__END__
What I am curious about is, when I run the code without the line to make value equal
null if there it is undefined, I get a lot of errors...
Use of Uninitialized Values.. Blah Blah.
When I add the line in.. the number of errors is reduced, but I still get them.
Why do I get them. And how do I get rid of them. The source files are ascii files
and I plan on entering each record into a database, then creating a worksheet in excel
out of it.
THanks in Advance for your time.
Will
CRS DEMO/12-12-1997/14:00:29
X1/FINANCIAL /12-12-1997/21:02
COUNT TOTAL
+PLU TTL 32.00 52.28
ADJST TTL 32.00 52.28
NONTAX 1 1.00
TAX1 SALES 0 44.99
TAX2 SALES 0 1.09
TAX3 SALES 0 1.29
TAX1 0 2.25
XMPTl SALES 0 0.25
EATIN TTL 1 5.24
TAKEOUT TTL 1 0.25
% 1 3 -1.57
% 2 2 -1.11
% 3 1 -4.94
NET SALE 6 48.49
RETURN 4 -6.98
GROSS SALES 0 63.09
CASH SALES 6 48.49
CASH- IN-D 6 48.49
DRWR TTL 0 48.49
REPORT END
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>