title: split array in sections
body: <p>I have an array with this type of content</p>
  
  <pre><code>a/a/a/test134.html
  a/a/a/test223.html
  a/b/b/test37.html
  a/b/test41.html
  a/b/test44.html
  a/b/test432.html
  a/d/test978.html
  a/test.html
  </code></pre>
  
  <p>I need to split it by "directories" so that I can send each array for a directory into a function (please see code sample).</p>
  
  <pre><code>a/a/a/test134.html
  a/a/a/test223.html
  
  a/b/b/test37.html
  
  a/b/test41.html
  a/b/test44.html
  a/b/test432.html
  
  a/d/test978.html
  
  a/test.html
  </code></pre>
  
  <p>This is what I have so far but I feel theres lots of bugs especially on end and beginning cases and is not clean enough to my liking.</p>
  
  <pre><code>for(my $i = 0; $i &lt; scalar(@arrayKeys); $i++)
  {
      my($filename, $directory) = fileparse($arrayKeys[$i]);
      my $currDir = $directory;
  
      # $currDir ne $prevDir: takes care of changes in path
      # $i + 1 == scalar(@arrayKeys): accounts for last row to be purged
      if($currDir ne $prevDir || $i + 1 == scalar(@arrayKeys))
      {   
          # if last row we need to push it
          if($i + 1 == scalar(@arrayKeys))
          {
              push(@sectionArrayKeys, $arrayKeys[$i]);
          }
  
          # ensure for first entry run we don't output
          if ($prevDir ne "")
          {
              &amp;output(\@sectionArrayKeys);
          }
  
          # Clear Array and start new batch
          @sectionArrayKeys = ();
          push(@sectionArrayKeys, $arrayKeys[$i]);        
      }
      else
      {
          push(@sectionArrayKeys, $arrayKeys[$i]);        
      }
  
      $prevDir = $currDir;
  }
  </code></pre>