convert structured strings to possibly deep hash of hashes



Hi,

I have a list of well structured strings, actually they are file paths.
This just measn there are strings of '/' seperated sub-strings. I can
easily split these into an array. My question is really one of
building a HoH based on all of the string records. My goal is to take
strings like:

/file.txt
/a/file.txt
/a/b/c
/a/b/c/file.txt
/z/m/w/file.txt

and produce something like:

%dir_hash(
'file.txt' => '',
'a' => {
'file.txt' => '',
'b' => {
'c' => {
'file.txt'
}
}
},
'z' => {
'm' => {
'w' => {
'file.txt' => ''
}
}
}
)

My current feeble attempt was the following. I am getting lost in the
"\%"s and the "%{}"s. To pre-answer some obvious suggestions: yes, I
have read perlref and perlreftut and my perl book(s) but I am not
seeing how to recursibely build/populate a HOH in this situation. I
have looked through CPAN and did find the IO::Dir module which might
help if the data was not static and i was pulling it from the
originating directory instead of a flat file.

suggestions?



#!/usr/local/bin/perl -w

use strict;
select(STDOUT);
$|++;

use Data::Dumper;

my @path = ();
my %path_hash = ();

@path = split('/', "/a/b/c/d/e/f/g.ics");
shift(@path) if ($path[0] eq "");

&make_path_hash(\%path_hash, @path);

print STDOUT Dumper(%path_hash);


sub make_path_hash
{
my ($path_hash, $this, @list) = @_;

return if (! defined($this));

$this =~ s/^\s+//s;
$this =~ s/\s+$//s;

return if ($this eq "");

$path_hash->{$this} = 1 if (! defined($path_hash->{$this}));

&make_path_hash(\%{ $path_hash->{$this} }, @list);
}

.



Relevant Pages

  • Re: Could not load or run "C:Users
    ... Check the run keys in the registry for strings that reference those file paths. ... Run regedit from the start/search line, expand and examine these keys: ...
    (microsoft.public.windows.vista.performance_maintenance)
  • Oracle StoredProc from VB 6.0
    ... I am trying to find out how to Call Oracle Stored Proc from VB6.0 + ... ado that takes in three strings as IN parameters and outputs an array ... of records or result set having file names and file paths. ...
    (comp.databases.oracle.server)
  • Oracle StoredProc from VB 6.0
    ... I am trying to find out how to Call Oracle Stored Proc from VB6.0 + ... ado that takes in three strings as IN parameters and outputs an array ... of records or result set having file names and file paths. ...
    (microsoft.public.data.ado)
  • Help - Counting text - Associative Array?
    ... Perl, but I have only been able to find bits and pieces on it. ... specifying the descriptions and the strings in the array ... TIA, Sam ... Try to count the occurance of file paths ...
    (comp.lang.perl)