in reply to Everybody loves not using File::Find

Here comes the version with File::Find, although "Everybody loves not using File::Find" :-) (I only tested it on Win. At home, no UNIX around. I expect some small modifications to satisfy UNIX, such like symlinks etc.)
use File::Find; use Data::Dumper; my $files = {}; find(sub {wanted($files);}, "c:/perl58"); print Dumper($files); sub wanted { if (($_ ne ".") && ($_ ne "..")) { insert(shift); } } sub insert { my @elements = split("/", $File::Find::name); my $parent = shift; my $index; foreach ($index = 0; $index <= $#elements; $index ++) { if (($index == $#elements) && (-f $_)) { $parent->{$elements[$index]} = $File::Find::name; } else { if (!defined($parent->{$elements[$index]})) { $parent->{$elements[$index]} = {}; } $parent = $parent->{$elements[$index]}; } } }

Replies are listed 'Best First'.
Re: Re: Everybody loves not using File::Find
by submersible_toaster (Chaplain) on Nov 27, 2002 at 05:44 UTC

    ++pg for headspin value ! This code does run on linux , a small mod to &wanted to make it symlink safe(r)

    if (($_ ne ".") && ($_ ne "..") && (! -l $_)) {

    However when the hash is unrolled, the first key is null, and keys are generated all the way down to the actual search base.

    eg: running this script on /usr/local/html/users/me returns VAR1='' => { 'usr'=> { 'local' => { 'html' => { 'me' => { etc,etc. Before the real search begins.

    So to accomodate for that we can change the assignment to @elements

    @elements = split("/" , substr($File::Find::name, length($ARGV[0])));
    Obviously that shift @ARGV had to be replaced with $ARGV[0] , which might be better done assigning to a (heaven forefend) global.

    'b'x2 || ! 'b'x2