FAQ

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

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

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

People

Translate

site design / logo © 2021 Grokbase