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,

From: John W. Krahn <jwkrahn@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->{'Scrip'}= $trades->{'Scrip'};
$singletrade->{'Shares'}= $trades->{'Shares'};
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;

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

-- To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

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

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 6 of 10 | next ›
Discussion Overview
groupbeginners @
postedOct 9, '09 at 5:14a
activeOct 9, '09 at 12:09p



site design / logo © 2021 Grokbase