in reply to directory listing to array tree
use strict; use warnings; use Data::Dumper qw( Dumper ); sub add { my $p = \shift; $p = \($$p->{$_}) for @_; } my $tree; while (<DATA>) { chomp; add $tree, m{[^/]+}g; } print(Dumper($tree)); __DATA__ /var/www/data/stuff /var/www /var/www/data/misc /var/logs /var/logs/data
$VAR1 = { 'var' => { 'www' => { 'data' => { 'misc' => undef, 'stuff' => undef } }, 'logs' => { 'data' => undef } } };
Golfed:
perl -MData::Dumper -ne'END{print Dumper$t}chomp;$p=\$t;$p=\($$p->{$_})for/[^\/]+/g'
Or if you don't mind a trailing blank line:
perl -MData::Dumper -nlE'END{say Dumper$t}$p=\$t;$p=\($$p->{$_})for/[^\/]+/g'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: directory listing to array tree
by contradev (Monk) on Sep 14, 2009 at 20:11 UTC | |
by Anonymous Monk on Sep 15, 2009 at 06:42 UTC |