title: Finding common elements in arrays
body: <p>I have a hash whose values are arrays. I need to find the common elements of those arrays, 
  ie. the elements that are present in all the arrays. So I extracted the values of the hash into 
  a multidimensional array whose each row corresponds to an array in the hash. Then I took the first row 
  of this matrix into another array (@arr1) and iterated through it to find if there was any element 
  in arr1 that was also in the rest of the rows of the matrix. If such an element is found, it is 
  pushed onto another array that contains the final list of all the elements. The code is as follows
  (I hope it is clear enough):</p>
  
  <pre><code>sub construct_arr(my %records) {
      my $len = keys %records;
      my @matrix;
      my $i = 0;
  
      # Extract the values of the hash into a matrix
      foreach my $key (keys %records) {
          $matrix[$i] = $records{$key};
          $i++;   
      }
  
      my @arr1 = $matrix[0];
      my @final;
  
      # Iterate through each element of arr1
      for my $j (0..$#{$arr1[0]}) {
          my $count = 1;
  
          # Iterate through each row of the matrix, starting from the second
          for ( my $i = 1; $i &lt; $len ; $i++ ) {
              my $flag = 0;
  
              # Iterate through each element of the row
              for my $k (0..$#{$matrix[$i]}) {
                  if ($arr1[0][$j] eq $matrix[$i][$k]) {
                      $flag = 1;
                      $count++;
                  }
              }
  
              # On finding the first instance of the element in a row, go to the next row
              if (!$flag == 1) {
                  last;
              }       
          }
  
          # If element is in all the rows, push it on to the final array
          if ($count == $len) {
              push(@final, $arr1[0][$j]);
          }
      }
      return @final;
  }
  </code></pre>
  
  <p>I know that the above works, but I would like to know if there is any other (perlish) way to do this.
  I am starting to learn perl and I am very interested in knowing things that could make my work easier 
  in perl as compared to other languages. If my code is the best that can be done, please let me know that
  too. Any guidance would be appreciated. Thanks!</p>