By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

HashMaps

P: 4
Hi,

I have a Hashmap called %prevDelivered() and it has key as pty_id and other infomation.

I wnat to read the above hasmap key as well as information into new hashmap called %lastDelivered().

I tried it with the follwoing code. but i am not able to move achive the required functionality.

Please suggest alternative code
Expand|Select|Wrap|Line Numbers
  1.  foreach (keys(%prevDelivered)) {
  2.              my $ptyid = $_;
  3.              my ($store_id, $pty_id, $trans_cd, $curr_status, $tdlinx_cd, $cat_id,
  4.                    $crc_mod1, $crc_mod2, $crc_mod3, $crc_mod4, $crc_mod5,
  5.                    $crc_mod6, $crc_mod7, $crc_mod8, $inc_in_file)
  6.                                                                       = @{ $prevDelivered{$ptyid} };
  7.              $lastDelivered{$ptyid} = ($store_id, $pty_id, $trans_cd, $curr_status, 
  8.                                                  $tdlinx_cd, $cat_id, $crc_mod1, $crc_mod2,
  9.                                                  $crc_mod3, $crc_mod4, $crc_mod5,  
  10.                                                  $crc_mod6, $crc_mod7, $crc_mod8, 
  11.                                                  $inc_in_file);
  12. }
  13.  
Oct 3 '08 #1
Share this Question
Share on Google+
9 Replies


Ganon11
Expert 2.5K+
P: 3,652
Hi,

I have a Hashmap called %prevDelivered() and it has key as pty_id and other infomation.

I wnat to read the above hasmap key as well as information into new hashmap called %lastDelivered().

I tried it with the follwoing code. but i am not able to move achive the required functionality.

Please suggest alternative code

Expand|Select|Wrap|Line Numbers
  1. foreach (keys(%prevDelivered)) {
  2.              my $ptyid = $_;
  3.              my ($store_id, $pty_id, $trans_cd, $curr_status, $tdlinx_cd, $cat_id,
  4.                    $crc_mod1, $crc_mod2, $crc_mod3, $crc_mod4, $crc_mod5,
  5.                    $crc_mod6, $crc_mod7, $crc_mod8, $inc_in_file)
  6.                                                                       = @{ $prevDelivered{$ptyid} };
  7.              $lastDelivered{$ptyid} = ($store_id, $pty_id, $trans_cd, $curr_status, 
  8.                                                  $tdlinx_cd, $cat_id, $crc_mod1, $crc_mod2,
  9.                                                  $crc_mod3, $crc_mod4, $crc_mod5,  
  10.                                                  $crc_mod6, $crc_mod7, $crc_mod8, 
  11.                                                  $inc_in_file);
  12. }
Expand|Select|Wrap|Line Numbers
  1. $lastDelivered{$ptyid} = ($store_id, $pty_id, $trans_cd, $curr_status, $tdlinx_cd, $cat_id, $crc_mod1, $crc_mod2, $crc_mod3, $crc_mod4, $crc_mod5, $crc_mod6, $crc_mod7, $crc_mod8, $inc_in_file);
This is the problematic line. You are trying to assign a list to a hash's value, which can only take a scalar. Before, you treated that hash's value as a reference to an array, so I assume you want to do the same thing here. Then instead of parentheses (), use square brackets []:


Expand|Select|Wrap|Line Numbers
  1. $lastDelivered{$ptyid} = [$store_id, $pty_id, $trans_cd, $curr_status, $tdlinx_cd, $cat_id, $crc_mod1, $crc_mod2, $crc_mod3, $crc_mod4, $crc_mod5, $crc_mod6, $crc_mod7, $crc_mod8, $inc_in_file];
Oct 3 '08 #2

P: 4
Expand|Select|Wrap|Line Numbers
  1. $lastDelivered{$ptyid} = ($store_id, $pty_id, $trans_cd, $curr_status, $tdlinx_cd, $cat_id, $crc_mod1, $crc_mod2, $crc_mod3, $crc_mod4, $crc_mod5, $crc_mod6, $crc_mod7, $crc_mod8, $inc_in_file);
This is the problematic line. You are trying to assign a list to a hash's value, which can only take a scalar. Before, you treated that hash's value as a reference to an array, so I assume you want to do the same thing here. Then instead of parentheses (), use square brackets []:


Expand|Select|Wrap|Line Numbers
  1. $lastDelivered{$ptyid} = [$store_id, $pty_id, $trans_cd, $curr_status, $tdlinx_cd, $cat_id, $crc_mod1, $crc_mod2, $crc_mod3, $crc_mod4, $crc_mod5, $crc_mod6, $crc_mod7, $crc_mod8, $inc_in_file];
Thanks a lot i got the required result now with above code.

I have 2 category ids as 34 and 35 and it have around 100 and 200 records on those ids.

When i repeat the below loop. i am getting final result on %lastDelivered hashmap as 200 records. But i am expecting total 300 records.

How can i hold the data on %lastDelivered hashmap for each iteration?
Expand|Select|Wrap|Line Numbers
  1. foreach ( @keys ) { //* Cat id 34 and 35 *//
  2.  
  3.        my $category = $_;
  4.  
  5.        GetLastStoreDelivered( $dbh, $delivery_id,$category, \%prevDelivered );
  6. //** Pull forst 100 records for the id 34 and move the data into %lastdelivered() *//
  7.  
  8.        foreach (keys(%prevDelivered)) {
  9.  
  10.             my $ptyid = $_;
  11.  
  12.             my ($store_id, $pty_id, $trans_cd, $curr_status, $tdlinx_cd, $cat_id, 
  13.                   $crc_mod1, $crc_mod2, $crc_mod3, $crc_mod4, $crc_mod5,  
  14.                   $crc_mod6, $crc_mod7, $crc_mod8, $inc_in_file) = 
  15.                                                                           @{ $prevDelivered{$ptyid} };
  16.  
  17.            $lastDelivered{$ptyid} = [$store_id, $pty_id, $trans_cd, $curr_status,  
  18.                                                $tdlinx_cd, $cat_id, $crc_mod1, $crc_mod2, $
  19.                                                crc_mod3, $crc_mod4, $crc_mod5,  
  20.                                                $crc_mod6,  $crc_mod7, $crc_mod8, 
  21.                                                $inc_in_file];
  22.  
  23.        }
  24.  
  25. }
  26.  
Oct 3 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Is $ptyid always a unique value? If not any duplicates will be lost. If it is always a unique value there is no way to tell by looking at the code you posted why you get 200 records instead of the expected 300.

Edit:

Never mind. $ptyid is the keys of the hash %prevDelivered so they must be unique at that point in the script. Maybe the problem is before the loop you posted.
Oct 3 '08 #4

P: 4
Is $ptyid always a unique value? If not any duplicates will be lost. If it is always a unique value there is no way to tell by looking at the code you posted why you get 200 records instead of the expected 300.

Edit:

Never mind. $ptyid is the keys of the hash %prevDelivered so they must be unique at that point in the script. Maybe the problem is before the loop you posted.

No, I am repeating the loop twice as per category ids 34 and 35. Fisrt id 34 will execute in loop and move 100 records into %lastDelivered() hashmap.

In next run id 35 will execute and overwrite 200 records on %lastDelivered() hashmap. which means i am loosing first 100 records and i am having last run data alone.

My question is here how can we hold previous run data on Hasnmap?
Oct 3 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
Its very difficult to say because I can't run any code, don't know what the data is, and don't know what your script is doing. But here is a hopefully educated guess, use an array of arrays instead of a hash key that stoes only one array per id:

change this line:

Expand|Select|Wrap|Line Numbers
  1. $lastDelivered{$ptyid} = [$store_id, $pty_id, $trans_cd, $curr_status,
  2. $tdlinx_cd, $cat_id, $crc_mod1, $crc_mod2, $
  3. crc_mod3, $crc_mod4, $crc_mod5,
  4. $crc_mod6, $crc_mod7, $crc_mod8,
  5. $inc_in_file];
  6.  
change to:

Expand|Select|Wrap|Line Numbers
  1. push @{$lastDelivered{$ptyid}}, [$store_id, $pty_id, $trans_cd, $curr_status,
  2. $tdlinx_cd, $cat_id, $crc_mod1, $crc_mod2, $
  3. crc_mod3, $crc_mod4, $crc_mod5,
  4. $crc_mod6, $crc_mod7, $crc_mod8,
  5. $inc_in_file];
  6.  
that means you will also have to change how you parse/access the data stored in $lastDelivered{$ptyid}. You will have to loop through it to get all the arrays, something like:

Expand|Select|Wrap|Line Numbers
  1. foreach my $array ( @{$lastDelivered{$ptyid}} ) {
  2.     print "@{$array}\n";
  3. }
Oct 3 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
You might be able to simplify the foreach loop to this:

Expand|Select|Wrap|Line Numbers
  1.    foreach (keys(%prevDelivered)) {
  2.       push @{$lastDelivered{$ptyid}}, $prevDelivered{$ptyid};
  3.  
  4.    }
  5.  
then dereference $prevDelivered{$ptyid} array later when you loop through %lastDelivered.
Oct 3 '08 #7

numberwhun
Expert Mod 2.5K+
P: 3,503
Thanks a lot i got the required result now with above code.

I have 2 category ids as 34 and 35 and it have around 100 and 200 records on those ids.

When i repeat the below loop. i am getting final result on %lastDelivered hashmap as 200 records. But i am expecting total 300 records.

How can i hold the data on %lastDelivered hashmap for each iteration?
Expand|Select|Wrap|Line Numbers
  1. foreach ( @keys ) { //* Cat id 34 and 35 *//
  2.  
  3.        my $category = $_;
  4.  
  5.        GetLastStoreDelivered( $dbh, $delivery_id,$category, \%prevDelivered );
  6. //** Pull forst 100 records for the id 34 and move the data into %lastdelivered() *//
  7.  
  8.        foreach (keys(%prevDelivered)) {
  9.  
  10.             my $ptyid = $_;
  11.  
  12.             my ($store_id, $pty_id, $trans_cd, $curr_status, $tdlinx_cd, $cat_id, 
  13.                   $crc_mod1, $crc_mod2, $crc_mod3, $crc_mod4, $crc_mod5,  
  14.                   $crc_mod6, $crc_mod7, $crc_mod8, $inc_in_file) = 
  15.                                                                           @{ $prevDelivered{$ptyid} };
  16.  
  17.            $lastDelivered{$ptyid} = [$store_id, $pty_id, $trans_cd, $curr_status,  
  18.                                                $tdlinx_cd, $cat_id, $crc_mod1, $crc_mod2, $
  19.                                                crc_mod3, $crc_mod4, $crc_mod5,  
  20.                                                $crc_mod6,  $crc_mod7, $crc_mod8, 
  21.                                                $inc_in_file];
  22.  
  23.        }
  24.  
  25. }
  26.  
Ruhee070806,

Ok, this is twice now that I have corrected your not using code tags. Please read this sites Posting Guidelines and use the proper code tags from now on. They are not optional.

Regards,

Jeff
(Moderator)
Oct 3 '08 #8

P: 4
Ruhee070806,

Ok, this is twice now that I have corrected your not using code tags. Please read this sites Posting Guidelines and use the proper code tags from now on. They are not optional.

Regards,

Jeff
(Moderator)
Thanks a lot and now i am able to hold the data into new Hashmap for each iteration.

I got struck into one more problem. I have around 1.5 million data needs to pul from database using Perl module and put those data into Hashmap.

Expand|Select|Wrap|Line Numbers
  1.  
  2. sub GetLastStoreDelivered
  3. {
  4.     my $sql = <<EOS;
  5. SELECT store_id
  6.   , party_id
  7.   , curr_status
  8.   , cat_id
  9.   , mod1
  10.   , mod2
  11.   , mod3
  12.   , mod4
  13.   , mod5
  14.   , mod6
  15.   , mod7
  16.   , mod8
  17. FROM client
  18. WHERE end_dt = '31-DEC-2500'
  19. EOS
  20.  
  21.     my $sth = $dbh->prepare( $sql );
  22.     if ( !$sth ) {
  23.         LogMsg( "Error preparing SQL:\n$sql" . DBI->errstr );
  24.         die "Could not prepare statement: " . DBI->errstr;
  25.     }
  26.  
  27.     if ( !$sth->execute() ) {
  28.         LogMsg( "Error executing SQL:\n$sql" . DBI->errstr );
  29.         die "Could not execute: " . DBI->errstr;
  30.     }
  31.  
  32.     while ( my @row = $sth->fetchrow_array() ) {
  33.         my $pty_id = $row[1];
  34.         $$delivered{$pty_id} = [ @row ];
  35.     }
  36.  
  37.     $sth->finish;
  38.     return $delivered;
  39. }
  40.  
  41.  
I am getting out-of-memory error here.....

And data will increase on Client table every month.

Can you please let me know how to overcome on this. Is we need to add more cpus to my system or is any other solution which exits.
Oct 6 '08 #9

KevinADC
Expert 2.5K+
P: 4,059
You need more memory. Or you need to free up memory. Memory as in system memory, RAM.
Oct 6 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.