title: Transform/pivot array in perl
body: <p>Im stuck writing Perl code which transforms 2d array.</p>
  
  <ul>
  <li>First column of the array is always date</li>
  <li>Second column of the array is key that sorts.</li>
  <li><p>Data is located in array "data" and is ordered by date and then key.</p></li>
  <li><p>My situation should be understandable from the tables under. Unique values from the second column will be selected and later divided into columns header (green table)</p></li>
  </ul>
  
  <p>It should work with and number of columns or dates/keys.</p>
  
  <p>Structure before</p>
  
  <p><img src="https://i.stack.imgur.com/mpK33.png" alt="structure before"></p>
  
  <p>Structure after</p>
  
  <p><img src="https://i.stack.imgur.com/is3nV.png" alt="structure after">
  My code:</p>
  
  <pre><code>#creates filtered array of all the unique dates and its count
  my @date          = @{ $data-&gt;[0] };
  my @filtDate      = uniq @date;
  my $countFiltDate = scalar @filtDate;
  #unique list of keys
  my @klic     = @{ $data-&gt;[1] };
  my @filtKlic = uniq @klic;
  #orders filtered keys
  @filtKlic = sort @filtKlic;
  my $countFiltKlic = scalar @filtKlic;
  #count of columns
  my $columnsCount = scalar @{$data};
  #test code - counts how many new number columns to make.
  my $columnsCountAfter = ( $columnsCount - 2 ) * $countFiltKlic;
  
  #inserst filtered dates into first column
  my $dataGraph;
  for ( my $i = 0; $i &lt; $countFiltDate; $i++ ) {
      $dataGraph-&gt;[0]-&gt;[$i] = @filtDate[$i];
  }
  
  #biggest loop with number of dates
  for ( my $k = 0; $k &lt; $countFiltDate; $k++ ) {
      my $l;
      my $c;
      #columns sount k $i
      for ( my $i = 0; $i &lt; $columnsCount - 2; $i++ ) {
          #loop for different keys k $j
          for ( my $j = 0; $j &lt; $countFiltKlic; $j++ ) {
              $l++;    #riadok v prvej tabulke
                       #EVERYTHING after this part is written horibly.
                       # I'm trying to make it work even
                       #if key values are missing.
              for ( my $m = 0; $m &lt; 5; $m++ ) {
                  if ( $data-&gt;[1]-&gt;[ $l - 1 ] eq $filtKlic[$j] ) {
                      print " [" . $data-&gt;[1]-&gt;[ ( $l - 1 ) ] . ',' . $filtKlic[$j] . "]";
                      $dataGraph-&gt;[ $l + $c ]-&gt;[$k] = $data-&gt;[ $i + 2 ]-&gt;[ ( ( $k * $countFiltKlic ) + $j ) ];
                      #print " [".$data-&gt;[1]-&gt;[$j].','.($filtKlic[($j)])."]-";
                      print " [" . ( $i + 2 ) . ',' . ( ( $k * $countFiltKlic ) + $j ) . "]-";
                      print " [" . ( $l + $c ) . ',' . $k . "]&lt;br&gt;";
                      $m = 5;    #just random number... i don't want to get infinite loops during testing
  
                  } else {
                      if ( $m = 5 ) {
                          $l--;
                          $c++;
                      }
                      $j++;
                  }
              }
          }
      }
  }
  
  my @nameStlpceKlic;
  @nameStlpceKlic[0] = "date";
  my $o;
  for ( my $i = 0; $i &lt; $columnsCount - 2; $i++ ) {
      foreach (@filtKlic) {
          my $o;
          $o++;
          @nameStlpceKlic[$o] = @filtKlic[ ( $o - 1 ) ] . $i;
      }
  }
  </code></pre>
  
  <p>I have 2 problems.</p>
  
  <ol>
  <li>How to make sure that this will work even if some of the key are missing at some dates.</li>
  <li>How to write it properly. My code is too clumsy.</li>
  </ol>