473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HashMaps

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
9 2275
Ganon11
3,652 Expert 2GB
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
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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
4,059 Expert 2GB
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
3,509 Expert Mod 2GB
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
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
4,059 Expert 2GB
You need more memory. Or you need to free up memory. Memory as in system memory, RAM.
Oct 6 '08 #10

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

Similar topics

3
by: Steve Johnson | last post by:
Been banging my head on this for two days now. Hope someone can help! My test program below is in the form of a single JSP, with a Node class build in. (All the coded needed to run is below.) ...
1
by: Marilyn Hart | last post by:
Hi I hope there is someone out there that may be able to help me. I have a Hashmap called that contains Bank Account objects, it is made up as follows. Bank Acc Number - int Bank Acc Type...
10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
7
by: Pino | last post by:
Hi, to everybody, let's consider this scenario: you have 1 data-table and 10 dictionary-tables; the data-table has 5 million records and 30 columns, 10 of these columns have a foreign-key to the...
2
by: Lee | last post by:
Hi, are there equivalents in c# as java vectors and hashmaps? thanks lee
13
by: shiniskumar | last post by:
Ive got a collection that is populated by some key/value pairs. How can i iterate ie to get its each key/value pair?
1
by: milanjain | last post by:
Merge two HashMaps into one in asceding order
4
by: namrata mukherjee | last post by:
i have a Hashmap whose value is an array list which in turn is a Hashmap.please tell me how to implement this in code
1
by: Peter Morris | last post by:
I wasn't expecting this... (Time in MS) Linq 858 Sort 9721 Dict 316 Linq - Sort = -8863 Linq - Dict = 542 I was originally writing something like the dictionary one but then I
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.