Also sprach Zebee Johnstone:
In comp.lang.perl.misc on Tue, 24 Aug 2004 09:57:17 +0200
Tassilo v. Parseval said:
Also sprach Zebee Johnstone:
I have a unix directory path, say /home/user/mail
Not knowing how long it will be, that is, how many elements, how can I
convert it into a sequence of hashes:
$ref->{'home'}->{'user'}->{'mail'}
This can be done with a simple recursive function:
sub path2hash {
my ($p, $ref) = @_;
return if not $p;
my ($head, $tail) = $p =~ m!/?([^/]+)(.*)!;
path2hash($tail, $ref->{ $head } = {});
I don't understand what you are passing here, and how the subroutine
sees it.
It's a short-cut. More explicitely:
$ref->{ $head } = { };
path2hash($tail, $ref->{ $head });
In Perl, assignments have a return value and that was what I was using
here.
Other than that, it should be pretty straight-forward. Note that this is
a so called primitive recursion because each instantiation of the
function cuts off a piece of its argument ($head) and calls path2hash
with the thusly diminshed argument ($tail).