Monday 30 September 2019

How to assign more than one arrays to crate a complete hash in perl?


Method 1[Faster]:


                     %final=();
                    @first=(7,8,3,4);
                     @second=('a','s','d','f');
                     my %final;
                    @final{@first} = @second;
                     print %final;
  
Method 2[Using Loop Method 1]:

  
                    @first=(7,8,3,4);
                     @second=('a','s','d','f');
                     $z=0;
                     foreach(@first) {
                      $final{ $_ } = $second[$z];
                      $z++;
                      } 
                      print %final;


Method 3[Using Loop Method 2]:


                     %final=();
                     @first=(7,8,3,4);
                     @second=('a','s','d','f');
                     $z=0;
                     while( ($t) = each(@first)) {
                      $final{ $t } = $second[$z];
                      $z++;
                      }
                      print %final


  Method 4:
  
                    %final=();
                     @first=(7,8,3,4);
                     @second=('a','s','d','f');
                      foreach my $v1 (@first)
                      {
                           my $v2= shift @second;
                           $final{ $v1 } = $v2;
                       }
                      print %final;
  

  Method 5:               
  
                      %final=();
                     @first=(7,8,3,4);
                     @second=('a','s','d','f');
                      for($i = 0; $i < @first; $i++) {
                      $final{$first[$i]} = $second[$i];
                      }
                      print %final;
  

Method 6:
  
                    %final=();
                     @first=(7,8,3,4);
                     @second=('a','s','d','f');
                     while( @first || @second)
                     {
                            my $v1= shift @first || "lets-crack-perl-interview";
                            my $v2= shift @second || "lets-crack-perl-interview";
                            $final{$v1} = $v2;
                    }
                     print %final;
  

Method 7:

                  %final=();
                  @first=(7,8,3,4);
                  @second=('a','s','d','f');
                  while( @first && @second)
                 {
                         my $v1= shift @first;
                         my $v2= shift @second;
                         $final{$v1} = $v2;
                 }
                    print %final;
  

 Method 8:
  
                   %final=();
                   @first=(7,8,3,4);
                   @second=('a','s','d','f');
                   while (@first || @second) {
                          $final{shift @first} = shift @second;
                   }
                    print %final;
  

Method 9:

                     %final=();
                     @first=(7,8,3,4);
                     @second=('a','s','d','f');
                     $final{shift @first}=shift(@second) while @second;
  

Method 10:
  
                     %final=();
                     @first=(7,8,3,4);
                     @second=('a','s','d','f');
                     $final{shift @first}=shift(@second) for @second;
                      print %final;


 Method 11:[Light Speed]
  
                    %final=();
                     @first=(7,8,3,4);
                     @second=('a','s','d','f');
                      $i=0;
                      %final=map {$first[$i++],$_; } @second;

  
 Method 12:[Builtin Modules]
  
                use List::MoreUtils qw( zip );
                 @first=(7,8,3,4);
                 @second=('a','s','d','f');
                 my %final = zip @final, @second;

kaavannan perl blogspot

Labels: , , , , , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home