Soham Das wrote:
The Code:

use strict;
use warnings;
use Tie::Handle::CSV;

my $qbook = Tie::Handle::CSV->new('C:\Documents and Settings\Soham 
Das\Desktop\Quotes.csv',header=>1);
my $tradebook = Tie::Handle::CSV->new('C:\Documents and Settings\Soham 
Das\Desktop\Transactions.csv',header=>1);

my $dailytrade={};
my $portfolio={};
my $singletrade={};

You are declaring $singletrade in file scope from this point forward.


while(my $mktdates =<$qbook>)
{
    while(my $trades=<$tradebook>)
        {
            if($trades->{'Date'} eq $mktdates->{'Date'})
                {
                 $singletrade->{'Action'}=$trades->{'Action'};
                 $singletrade->{'Scrip'}= $trades->{'Scrip'};
                 $singletrade->{'Shares'}= $trades->{'Shares'};
                 $singletrade->{'Price'}=($trades->{'Price'})*1.00845;

Because $singletrade is in file scope you are overwriting the same four keys each time you enter this if block. You need to restrict the scope of $singletrade to inside the inner while loop otherwise you are just copying the *same* hash reference to every key of $dailytrade.


                 $dailytrade->{$singletrade->{'Scrip'}}= $singletrade;
#                 print $dailytrade->{$singletrade->{'Scrip'}}->{'Price'},"\n";
                }
                #Update the portfolio

        }
}
close $qbook ;
close $tradebook;

You probably want something like this:

use strict;
use warnings;
use Tie::Handle::CSV;

my $qbook = Tie::Handle::CSV->new( 'C:\Documents and Settings\Soham Das\Desktop\Quotes.csv', header => 1 ); my $tradebook = Tie::Handle::CSV->new( 'C:\Documents and Settings\Soham Das\Desktop\Transactions.csv', header => 1 );

my %dailytrade;
my %portfolio;
while ( my $mktdates = <$qbook> ) {

    while ( my $trades = <$tradebook> ) {

        if ( $trades->{ Date } eq $mktdates->{ Date } ) {

            $dailytrade{ $trades->{ Scrip } } = {
                Action => $trades->{ Action },
                Scrip  => $trades->{ Scrip },
                Shares => $trades->{ Shares },
                Price  => $trades->{ Price } * 1.00845,
                };

#           print "$dailytrade{$trades->{Scrip}}{Price}\n";
            }
        #Update the portfolio

        }
    }

close $qbook;
close $tradebook;



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to