Link to home
Start Free TrialLog in
Avatar of lonaj
lonaj

asked on

Total and sub total value for the period and by client name

For the script below;
A- How to resolve the Error message "Use of uninitialized value in concatenation (.) or string" ( I think it is todo with $ sign)
B- I need to know how I can get the TOTAL $ value for the period and SUBTOTAL $ value for the name for example
Francesca Voolvo 07/03/2005 710 $0.00 $153.00
Francesca Voolvo 07/03/2005 940 $0.00 $98.00
                           Total value for the period 07/03/2005 is $251    # that is ($153+98=251)
Francesca Voolvo 07/03/2004 940 $0.00 $198.00
                           Subtotal for Francesca is $449                         # subtotal is Total value above + $198= $49

Anna smith 08/02/2005 600 $0.00 $113.00
Anna smith 08/03/2005 640 $0.00 $1,109.00
                           Total value for the period 07/03/2005 is $1222
                            Subtotal for Anna is $1222

My script is

my $datafile= "c:/mydir/last.txt";
my $clients= "c:/mydir/clients.txt";
open( CLIENTS, "< $clients" ) or die "Can't open $clients : $!";
my @fields= ();
while($_= <CLIENTS> ){
@fields = split ' ';
my $customer = "$fields[0] $fields[1] $fields[2]";
my @mynames = "$fields[0] $fields[1] $fields[2]";
open( DATAFILE, "< $datafile" ) or die "Can't open $datafile : $!";
my @datafile= ();
while($_ = <DATAFILE> ){
  if(/$customer/) {
@datafile = split ' ';
print  "$datafile[0] $datafile[1] $datafile[2] $datafile[5] $datafile[6] $datafile

[7] $datafile[8]\n";
}
}
close DATAFILE;
}
close CLIENTS;

-------------------------------------
THE CURRENT OUTPUT OF THE SCRIPT IS

Francesca Voolvo 07/03/2005 710 $0.00 $153.00
Francesca Voolvo 07/03/2005 940 $0.00 $98.00
Francesca Voolvo 07/03/2004 940 $0.00 $198.00
Anna smith 08/02/2005 600 $0.00 $113.00
Anna smith 08/03/2005 640 $0.00 $1,109.00
john WWWW 23/02/2006 600 $0.00 $113.00
Use of uninitialized value in concatenation (.) or string at jj5.pl line 15, <DA
TAFILE> line 803.
Use of uninitialized value in concatenation (.) or string at jj5.pl line 15, <DA
TAFILE> line 803.
john WWWW 16/03/2006 640
Jeffory plee 06/03/2006 600 $0.00 $113.00
Yacoub Mercie 08/02/2006 722 $0.00 $185.15
Florence floor 30/11/2005 700 $0.00 $59.00
Florence floor 30/11/2005 800 $0.00 $113.00
Florence floor 21/12/2005 840 $0.00 $387.00
MARTA lee 19/12/2005 722 $0.00 $0.00
MARTA lee 19/12/2005 940 $0.00 $0.00


Avatar of ozo
ozo
Flag of United States of America image

what is line 803 of in c:/mydir/last.txt?
is there really a blank line between $datafile
and
[7]

does c:/mydir/last.txt line 803 have 9 fields?
Avatar of lonaj
lonaj

ASKER

line 803 does not have field 7 and 8
803:          john WWWW        xxxxxxxx xxxxxx 16/03/2006     640

So if I take out fileds 7 and 8 (print  "$fields[1] $fields[2] $fields[5] $fields[6]\n";) I do not get the error message


does c:/mydir/last.txt line 803 have 9 fields?
a line in last.txt file may contain 9 fileds or more or less in this case is less
You might try
print "$fields[1] $fields[2] @fields[5..$#fields]\n";
which will print just the fields after 5 that do exist.
Would you want to consider fields that don't exist to be $0.00 for totaling purposes?

Is c:/mydir/clients.txt small enough to fit in memory so you don't have to open and read through the whole file multiple times?
is /$customer/ supposed to match particular fields in <DATAFILE>?  if so, which fields?
are the records in <DATAFILE> that match the same /$customer/ the records that you want grouped together for TOTAL and SUBTOTAL?
Avatar of lonaj

ASKER

Yes the records in <DATAFILE> that match the same /$customer/ the records that I want o groupe together for TOTAL and subtotal, (TOTAL by name, Date Service).

as per below (is /$customer/ supposed to match particular fields in <DATAFILE>?  if so, which fields?)
/$customer/ match  $fields[0] $fields[1] $fields[2] on <DATAFILE>

lients.txt contain the name of the clients for example
Mrs. Olga Doo
Mr Henry Fox
MRS. SERAH FOSTER

example of <DATAFILE>
some info xxxxxxx xxxxx
xxxxxx  xxxxx
filed0,1,2            field[3]        field[4]             field[5]       filed[6]  field[7]   filed[8]
Client name        Client_No     IDnumber-2     Date service   Item    GST        Benefit      
Mrs. Olga Doo    FAI0-105      2248A7721F    29/11/2005     722    $0.00        $0.00
Ms. Sue Jake       JK3930T      1234
xxxxxxxxxxSome data xxxxxxxxxxxx
some info xxxxxxx xxxxx
xxxxxx  xxxxx
Mr Henry Fox       KLM0-305      ABCD8A77    29/11/2005     992    $10.00        $350.00
Mr Henry Fox       KLM0-305      ABCD8A77    4/10/2005       878    $.00            $50.00
some data xxxxxxxxx xxxxx          xxxxxxxxxx          xxxxxxxx
 
Avatar of lonaj

ASKER

any help on this question please ?
my $datafile= "c:/mydir/last.txt";
my $clients= "c:/mydir/clients.txt";
open( DATAFILE, "< $datafile" ) or die "Can't open $datafile : $!";
my %data;
while( <DATAFILE> ){
    my @fields = split;
    push @{$data{"@fields[0..2]"}},[@fields[5..8]] if $#fields>=8;
}
close DATAFILE;
open( CLIENTS, "< $clients" ) or die "Can't open $clients : $!";
while($_= <CLIENTS> ){
    my @fields = split;
    if( my $data = $data{my $customer="@fields[0..2]"} ){
        my $total=0;
        my $subtotal=0;
        my $prevdate;
        for( @$data ){
            if( $prevdate && $prevdate ne $_->[0] ){
                printf("\tTotal value for the period %s is %.2f\n",$prevdate,$subtotal);
                $subtotal=0;
            }
            print "$customer @$_\n";
            my($v)=($_->[3]||0)=~/\D*([\d.]*)/;
            $subtotal += $v;
            $total += $v;
            $prevdate=$_->[0];
        }
        printf("\tTotal value for the period %s is \$%.2f\n",$prevdate,$subtotal) if $subtotal;
        printf("\tSubtotal for %s is \$%.2f\n",$customer,$total);
       
    }
}
close CLIENTS;
Avatar of lonaj

ASKER

Thank you OZO that works fine, Except when there is $1000 or more on field[8] (benefit) it print as $1 the result also subtotal will be wrong
see bellow for example for (Anna mils,John MOORE)

Ms. Anna mils 08/02/2006 600 $0.00 $113.00
        Total value for the period 08/02/2006 is 113.00
Ms. Anna mils 08/03/2006 640 $0.00 $1,109.00
        Total value for the period 08/03/2006 is $1.00 <------  from $1,109.00
        Subtotal for Ms. Anna VORBOJOV is $114.00   <----- $113+$1,109.00 ?

Mr. John MOORE 22/02/2006 600 $0.00 $113.00
        Total value for the period 22/02/2006 is 113.00
Mr. John MOORE 15/03/2006 640 $0.00 $1,215.00
        Total value for the period 15/03/2006 is $1.00 <------- from $1,215.00
        Subtotal for Mr. Eric MOORE is $114.00           <------- $113+$1,215.00 ?

MRS. JULIE Sink 25/01/2006 722 $0.00 $185.15
        Total value for the period 25/01/2006 is 185.15
MRS. JULIE Sink 07/03/2006 850 $0.00 $741.00
        Total value for the period 07/03/2006 is $741.00
        Subtotal for MRS. JULIE LOTTORTO is $926.15
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lonaj

ASKER

yes ozo that worked,thanks for all support and followup

Note** using the line $v=~/,//g; ## gives the following error message;

Unquoted string "g" may clash with future reserved word at mypl.pl line 27.
Useless use of division (/) in void context at mypl.pl line 27.
Argument "g" isn't numeric in division (/) at mypl.pl line 27, <CLIENTS> line 1

Illegal division by zero at mypl.pl line 27, <CLIENTS> line 1.

I have changed the line from $v=~/,//g; TO $v=~s/,//g; ## and worked fine