yes, I actually understood the problem but didnt have the "brainwave" to solve 
it. Now its much better. 
Yes indeed this will work much better.

Thank you,
John




________________________________
From: John W. Krahn <jwkr...@shaw.ca>
To: Perl Beginners <beginners@perl.org>
Sent: Fri, 9 October, 2009 11:57:09 AM
Subject: Re: Building a record on the fly via hash of hashes

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/


      From cricket scores to your friends. Try the Yahoo! India Homepage! 
http://in.yahoo.com/trynew

Reply via email to