Is this code better?
I noticed, you missed some inverted commas, 
So, at the risk of being pedantic, I am appending the code:

#!/usr/bin/perl
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,
                        };
                }
                #Update the portfolio

        }
}


close $qbook ;
close $tradebook;


When I compile this, it gives me an error like this:

Can't modify constant item in scalar assignment at Test.pl line 18, near "}";

So for the sake of clarity I highlited the Line 18.

'Action'=$trades->{'Action'},

A bit of help in this, will be great!

Soham




________________________________
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