Hello All,
I was wondering if someone could tell me how to manipulate this data structure. I
have attached the datafile. My code and code results are printed below.
If you take a look at the results, you will see that there is a hash created for each
interaction in the loop and the results are printed using data::dumper. The problem,
is that some hash values are blank, because they are printed during the following
interaction of the loop.
In other words. I am getting this...
$VAR1 = {
'waste' => '',
'promo' => '',
'count' => '',
'plu' => '00000000002800',
'total' => '',
'description' => '6 NUGGET',
'line' => 'PLU'
};
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '5.00',
'plu' => '',
'total' => '10.00',
'description' => '',
'line' => ''
};
and I want to get this...
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '5.00',
'plu' => '00000000002800',
'total' => '10.00',
'description' => '6 NUGGET',
'line' => 'PLU'
};
Could anyone help explain why this is occuring and how to resolve it. Thank you all
very much for reading my message.
PS. Does anyone know where I can get a tutorial or book chapter that talks about how
to use Data::Dumper. I checked the documentation on CPAN and it is difficult for me
to understand. Any ideas??
THANKS!!
<CODE>
#!/perl
use strict;
use warnings;
use Diagnostics;
use Data::Dumper 'Dumper';
#find the date report you need
#open the report
open(FILE,"SAM65 plu report.txt") or die("Can't open report");
my %record;
############################################################################################################
#print csv( {
#line => 'line',
#plu => 'plu',
#description => 'description',
#count => 'count',
#total => 'total',
#promo => 'promo',
#waste => 'waste'
# } ), "\n";
while(<FILE>){
# create record
my %record = (
line => '',
plu => '',
description => '',
count => '',
total => '',
promo => '',
waste => ''
);
if($.>3){
if(/^PLU/){
#print "$.starts with plu\n";
@record{ qw| line plu description |} = get_plu_and_description($_);
# delete($record{'count'});
# delete($record{'total'});
# delete($record{'promo'});
# delete($record{'waste'});
}
else{
@record{ qw| count total promo waste | }
= get_count_total_promo_waste($_);
}
#print Data::Dumper->Dump([\%record],[*record]);
print Dumper(\%record);
#print csv( \%record ), "\n" if is_valid_record( \%record );
}
}
############################################################################################################
sub get_info{
my $field = shift;
chomp($field);
$field =~ s/\t+/ /g;
my @fields = split(/' '/);
foreach my $each (@fields){
$each =~ s/^\s+//;
$each =~ s/\s+$//;
}
return $fields['line'];
}
sub get_count_total_promo_waste{
my $field = shift;
chomp($field);
$field =~ s/\t+/ /g;#replace all tabs with spaces
my($count, $total, $promo,$waste) = split(" ",$field);
return $count, $total, $promo, $waste;
}
sub get_plu_and_description{
my $field = shift;
chomp($field);
my($line, $plu, $description) = split(/\t/,$field);
return $line, $plu, $description;
}
sub is_valid_record {
my $field = shift;
foreach my $value ( values %$field ) {
# return true if one field is valid
return 1 if defined $value && $value ne '';
}
# 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| line plu description count total promo waste |;
# add double quotes (")
foreach my $value ( values %$field ) {
$value = "NULL" if(!defined($value));
$value =~ s/^\s+//;
$value =~ s/\s+$//;
$value = qq("$value");
}
# use a hash slice to order the fields
return join ',', @$field{ @field_order };
}
<END CODE>
<RESULTS>
$VAR1 = {
'waste' => '',
'promo' => '',
'count' => '',
'plu' => '00000000002800',
'total' => '',
'description' => '6 NUGGET',
'line' => 'PLU'
};
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '5.00',
'plu' => '',
'total' => '10.00',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => '',
'promo' => '',
'count' => '',
'plu' => '00000000002900',
'total' => '',
'description' => '9 NUGGET',
'line' => 'PLU'
};
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '5.00',
'plu' => '',
'total' => '14.95',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => '',
'promo' => '',
'count' => '',
'plu' => '00000000003000',
'total' => '',
'description' => '20 NUGGET',
'line' => 'PLU'
};
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '3.00',
'plu' => '',
'total' => '10.97',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => '',
'promo' => '',
'count' => '',
'plu' => '00000000003100',
'total' => '',
'description' => 'HOT DOG',
'line' => 'PLU'
};
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '6.00',
'plu' => '',
'total' => '0.99',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => '',
'promo' => '',
'count' => '',
'plu' => '00000000003200',
'total' => '',
'description' => 'POLISH',
'line' => 'PLU'
};
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '6.00',
'plu' => '',
'total' => '7.42',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => '',
'promo' => '',
'count' => '',
'plu' => '00000000003400',
'total' => '',
'description' => 'BRATWURST',
'line' => 'PLU'
};
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '2.00',
'plu' => '',
'total' => '2.57',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => undef,
'promo' => undef,
'count' => undef,
'plu' => '',
'total' => undef,
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => '',
'promo' => '',
'count' => '',
'plu' => '00000000003500',
'total' => '',
'description' => 'FISH',
'line' => 'PLU'
};
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '3.00',
'plu' => '',
'total' => '3.00',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => '',
'promo' => '',
'count' => '',
'plu' => '00000000003600',
'total' => '',
'description' => 'CHIC SAL',
'line' => 'PLU'
};
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '1.00',
'plu' => '',
'total' => '1.09',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => '',
'promo' => '',
'count' => '',
'plu' => '00000000003700',
'total' => '',
'description' => 'BBQ',
'line' => 'PLU'
};
$VAR1 = {
'waste' => '0.00',
'promo' => '0.00',
'count' => '1.00',
'plu' => '',
'total' => '1.29',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => '-',
'promo' => '-',
'count' => '-',
'plu' => '',
'total' => '-',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => '0.00',
'promo' => '52.28',
'count' => 'COLUMN',
'plu' => '',
'total' => 'TOTALS',
'description' => '',
'line' => ''
};
$VAR1 = {
'waste' => undef,
'promo' => undef,
'count' => 'REPORT',
'plu' => '',
'total' => 'END',
'description' => '',
'line' => ''
};
<END RESULTS>
CRS DEMO_12-12-1997/13:51:31
X1/PLU /12-12-1997/20:53
COUNT TOTAL PROMO WASTE
PLU 00000000002800 6 NUGGET
5.00 10.00 0.00 0.00
PLU 00000000002900 9 NUGGET
5.00 14.95 0.00 0.00
PLU 00000000003000 20 NUGGET
3.00 10.97 0.00 0.00
PLU 00000000003100 HOT DOG
6.00 0.99 0.00 0.00
PLU 00000000003200 POLISH
6.00 7.42 0.00 0.00
PLU 00000000003400 BRATWURST
2.00 2.57 0.00 0.00
PLU 00000000003500 FISH
3.00 3.00 0.00 0.00
PLU 00000000003600 CHIC SAL
1.00 1.09 0.00 0.00
PLU 00000000003700 BBQ
1.00 1.29 0.00 0.00
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
COLUMN TOTALS 52.28 0.00 0.00
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>