in reply to case insensitive filename matching

This maybe not as short as the former, but i hope itīs clearer. The code creates a tree from the filesystem, that is used to lookup directory names.

Assuming this fs-structure:
test ->trEE ->/A ->/b
this code:
use strict; use warnings; # hash for file-system data my %tree; #build a datastructure that represents directory tree #for everything below "/test" build_tree ("/test", \%tree); # get realpath of "c:/test/tree/a/b" my $p = getRealPath ("TREE/A/B", \%tree); print $p; sub build_tree { my $dir = shift; my $ref = shift; my $dirh = shift; opendir $dirh, $dir or die $!; while ( $_ = readdir ($dirh) ) { next if /^\./; next unless -d "$dir/$_"; $ref->{subdirs}->{lc($_)} = { name=>$_, subdirs=>{} }; build_tree ("$dir/$_", $ref->{subdirs}->{lc($_)}); } closedir $dirh; } sub getRealPath { my @path = split ("/", shift); my $ref = shift; my $path = ""; for ( @path ) { $path .= "/" . $ref->{subdirs}->{lc($_)}->{name}; $ref = $ref->{subdirs}->{lc($_)}; } return $path; }
prints:
test/trEE/A/B
holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: case insensitive filename matching
by Preceptor (Deacon) on Feb 14, 2005 at 10:22 UTC
    Thanks, that worked beautifully.

    Of course, now I've got to figure out how to deal with people putting horrible things like ampersands into their filenames, but that I'm sure I can deal with.