HashMaps

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ruhee070806
    New Member
    • Oct 2008
    • 4

    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
    Code:
     foreach (keys(%prevDelivered)) {
                 my $ptyid = $_;
                 my ($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)
                                                                          = @{ $prevDelivered{$ptyid} };
                 $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);
    }
    Last edited by numberwhun; Oct 3 '08, 02:54 PM. Reason: Please use code tags
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by Ruhee070806
    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

    Code:
    foreach (keys(%prevDelivered)) {
                 my $ptyid = $_;
                 my ($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)
                                                                          = @{ $prevDelivered{$ptyid} };
                 $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);
    }
    Code:
    $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 []:


    Code:
    $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];

    Comment

    • Ruhee070806
      New Member
      • Oct 2008
      • 4

      #3
      Originally posted by Ganon11
      Code:
      $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 []:


      Code:
      $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?
      Code:
      foreach ( @keys ) { //* Cat id 34 and 35 *//
      
             my $category = $_;
      
             GetLastStoreDelivered( $dbh, $delivery_id,$category, \%prevDelivered );
      //** Pull forst 100 records for the id 34 and move the data into %lastdelivered() *//
      
             foreach (keys(%prevDelivered)) {
      
                  my $ptyid = $_;
      
                  my ($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) = 
                                                                                @{ $prevDelivered{$ptyid} };
      
                 $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];
      
             }
      
      }
      Last edited by numberwhun; Oct 3 '08, 05:55 PM. Reason: Please use code tags

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #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.

        Comment

        • Ruhee070806
          New Member
          • Oct 2008
          • 4

          #5
          Originally posted by KevinADC
          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?

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            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:

            Code:
            $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];
            change to:

            Code:
            push @{$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];
            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:

            Code:
            foreach my $array ( @{$lastDelivered{$ptyid}} ) {
                print "@{$array}\n";
            }

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              You might be able to simplify the foreach loop to this:

              Code:
                 foreach (keys(%prevDelivered)) {
                    push @{$lastDelivered{$ptyid}}, $prevDelivered{$ptyid};
              
                 }
              then dereference $prevDelivered{ $ptyid} array later when you loop through %lastDelivered.

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                Originally posted by Ruhee070806
                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?
                Code:
                foreach ( @keys ) { //* Cat id 34 and 35 *//
                
                       my $category = $_;
                
                       GetLastStoreDelivered( $dbh, $delivery_id,$category, \%prevDelivered );
                //** Pull forst 100 records for the id 34 and move the data into %lastdelivered() *//
                
                       foreach (keys(%prevDelivered)) {
                
                            my $ptyid = $_;
                
                            my ($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) = 
                                                                                          @{ $prevDelivered{$ptyid} };
                
                           $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];
                
                       }
                
                }
                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)

                Comment

                • Ruhee070806
                  New Member
                  • Oct 2008
                  • 4

                  #9
                  Originally posted by numberwhun
                  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.

                  [Code=Perl]

                  sub GetLastStoreDel ivered
                  {
                  my $sql = <<EOS;
                  SELECT store_id
                  , party_id
                  , curr_status
                  , cat_id
                  , mod1
                  , mod2
                  , mod3
                  , mod4
                  , mod5
                  , mod6
                  , mod7
                  , mod8
                  FROM client
                  WHERE end_dt = '31-DEC-2500'
                  EOS

                  my $sth = $dbh->prepare( $sql );
                  if ( !$sth ) {
                  LogMsg( "Error preparing SQL:\n$sql" . DBI->errstr );
                  die "Could not prepare statement: " . DBI->errstr;
                  }

                  if ( !$sth->execute() ) {
                  LogMsg( "Error executing SQL:\n$sql" . DBI->errstr );
                  die "Could not execute: " . DBI->errstr;
                  }

                  while ( my @row = $sth->fetchrow_array () ) {
                  my $pty_id = $row[1];
                  $$delivered{$pt y_id} = [ @row ];
                  }

                  $sth->finish;
                  return $delivered;
                  }

                  [/Code]

                  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.
                  Last edited by eWish; Oct 6 '08, 09:03 PM. Reason: Fixed code tags

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    You need more memory. Or you need to free up memory. Memory as in system memory, RAM.

                    Comment

                    Working...