Reputation: 30956
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;
}
Upvotes: 1
Views: 153
Reputation: 67920
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:
use warnings;
use strict;
my %dir_arrays;
while (<DATA>) {
chomp;
if (m{^(.+/)([^/]+)$}) {
push @{$dir_arrays{$1}}, $_; # or use $2 for just filename
}
}
use Data::Dumper;
print Dumper \%dir_arrays;
__DATA__
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
Output:
$VAR1 = {
'a/b/' => [
'a/b/test41.html',
'a/b/test44.html',
'a/b/test432.html'
],
'a/d/' => [
'a/d/test978.html'
],
'a/b/b/' => [
'a/b/b/test37.html'
],
'a/a/a/' => [
'a/a/a/test134.html',
'a/a/a/test223.html'
],
'a/' => [
'a/test.html'
]
};
Now, to send these arrays to a function, do something like this:
for my $key (keys %dir_arrays) {
my_function($dir_arrays{$key}); # this sends an array reference
}
If you prefer to send an array instead of an array reference, just dereference it:
my_function(@{$dir_arrays{$key}});
Edit: Changed the script to store the full path, as it was more in line with the wanted output in the question.
Upvotes: 2