Purl said:
Your article is gibberish.
Precisely what do you want to count?
Work towards developing an ability to write articles
which are clear, concise, coherent and free of gibberish.
Purl Gurl
OK, is this better?
1. I have a list of files like:
file1
file2
dir1/file3
dir1/file4
dir1/subdir1/file5
dir2/file6
....
They are stored in an array, call it @listOfFiles:
@listOfFiles = qw(
file1
file2
dir1/file3
dir1/file4
dir1/subdir1/file5
dir2/file6
....
);
2. The files are already stored in an array. I do not need to call
File::Find to get them. They are in @listOfFiles.
3. The files are already stored in an array. I do not need to call
opendir...; readdir...; closedir...; They are in @listOfFiles.
4. All the files are relative paths. None start with a slash.
5. All the files follow the UNIX convention. None have their directories
separated by a backslash. None of them follow the MS-DOS method of
separating directories in a path. If a backslash appears in a file, it
is part of the name.
6. For each file in the list of files, I want to build a hash of hashes
of hashes, one level for each directory in the path, with the last level
being the basename of the file, that count the number of times the file
appears in the list. The exception is that if there is no directories,
assume it has one (and only one) called '.'; if the file is "file1"
treat it as "./file1"
7. In other words, if the file is "file1" then execute a command that
has the same effect as: $File{ '.' }{ 'file1' } ++;
8. If the file has exactly one directory, called 'dir1', execute a
command that has the same effect as: $File{ 'dir1' }{ 'file3' } ++;
9. If the file has exactly two directories, called 'dir1' and 'subdir1',
execute a command that has the same effect as:
$File{ 'dir1' }{ 'subdir1' }{ 'file5' } ++;
10. Etc.
11. The number of directories may be any number and is not known until
the program is run. The number of directories is undeterminate but finite.
12. Question: is there some Perl command, like map, or some Perl module
I can use in the following algorithm?
foreach my $file ( @listOfFiles )
{
my @directory_or_basename = split /\//, $file;
# add the dot directory if there are none.
unshift @directory_or_basename, '.'
if @directory_or_basename == 1;
my $refHash = \%File;
while( my $directory_or_basename = shift @directory_or_basename )
{
if( @directory_or_basename ) # if this list is not empty,
# $directory_or_basename must
# be a directory.
{
$refHash->{ $directory_or_basename } = {}
unless defined $refHash->{ $directory_or_basename };
$refHash = $refHash->{ $directory_or_basename };
}
else # the list is empty,
# therefore $directory_or_basename must be a basename.
{
$refHash->{ $directory_or_basename } ++;
}
}
}