I have an array with this type of content
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
I need to split it by “directories” so that I can send each array for a directory into a function (please see code sample).
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
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.
for(my $i = 0; $i < 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 "")
{
&output(\@sectionArrayKeys);
}
# Clear Array and start new batch
@sectionArrayKeys = ();
push(@sectionArrayKeys, $arrayKeys[$i]);
}
else
{
push(@sectionArrayKeys, $arrayKeys[$i]);
}
$prevDir = $currDir;
}
Your script is confusing, but from what I understand, you want to split the array of paths into new arrays, depending on their path. Well, easiest way to keep them apart is using a hash, like so:
Output:
Now, to send these arrays to a function, do something like this:
If you prefer to send an array instead of an array reference, just dereference it:
Edit: Changed the script to store the full path, as it was more in line with the wanted output in the question.