Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Ok, now i really have a brain teaser...

i am making a database. i basically am finished. now im making a program which links directories and categories. In this linker, i have to grab a directory tree and plug it into each config file for each directory. This is accomplished easily, but what is hard is the categories...

you see, there is a category name for each directory, and although i have a way of telling exactly what directory im in, i have no way of telling the full path of categories im in. Basically, i can tell what the current category for the current directory is, but i can't (correctly) get the FULL PATH of categories.

for instance, i know am in directory /webdbase/pc/sw and i know that it is category SoftWare but i dont know how to tell that i am in /Main/PC Stuff/SoftWare. I can record each succeeding category, but that only works if there's 1 directory in each directory (a straight line).

So i thought, maybe i could use a hash, but many of my directory names are the same so it wouldn't work right. so as i parse through different categories and directories, how can i keep them all in line?

To get the directories i made a subroutine that recursively searches for all dirs, then goes back and searches those dirs. it finds all dirs with "db_engine.conf" and puts them all into an array. then i just erase the "db_engine.conf" and i have the full directories.

if you dont get what im saying email me at psypete@phreaker.net and ill give you a copy of the linker so far if you need it.

Edit 2001-04-04 by tye

Replies are listed 'Best First'.
Re: My kingdom for a substring
by rchiav (Deacon) on Apr 04, 2001 at 18:40 UTC
    Without the code it's hard to comment. The only thing that I can offer of value is to ask if you're using File::Find.

    If not, I think it will do what you're looking for. $File::Find::name contains the full path to whatever you're working with at the moment. You pass it a sub to call for each file/dir it finds. Here's one that I use to purge all files older than 14 days.

    sub process_file { my($name, @contents); $name = $File::Find::name; #change /'s to 's for the attrib command on windows platforms $name =~ s#/#\\#g; if (!-l && -d _) { unless (@contents = <$File::Find::name/*>) { if (rmdir($File::Find::name)) { print LOGFILE "Directory $name empty - deleted.\n" +; } elsif ((!`attrib -R $name`) && (rmdir($File::Find::name))) + { print LOGFILE "Directory $name empty - deleted(Read On +ly).\n"; } else { print LOGFILE "ERR: Can't delete $name - $!\n"; } } } else { my ($age, $size, $max_age); $max_age = 14; $age = int( -M ) < int( -C ) ? int( -M ) : int( -C ); if ($age > $max_age) { $size = (-s); if (unlink($File::Find::name) ) { print "$File::Find::name, - $age days, $size \n"; print LOGFILE "$File::Find::name, - $age days, $size \ +n"; } elsif ((!`attrib -R $name`) && (unlink($File::Find::name)) +) { print LOGFILE "$File::Find::name, - $age days, $size ( +Read Only)\n\n"; } else { print LOGFILE "ERR: Couldn't delete $File::Find::name +- $!\n"; } } } }
    Hope that helps.. Rich
Re: My kingdom for a substring
by Masem (Monsignor) on Apr 04, 2001 at 18:42 UTC
    I would create a nest hash of hashes, such as :
    my %data = ( 'main' => 'Top Level', 'pc' => ( 'main' => 'PC Software', 'gfx' => ( 'main' => 'Graphics', ... ), .... ) ...);
    Namely, one fixed hash entry, in this case 'main', to be the catagory for that directory, and other entries as hashes to other sub directories.

    Thus, given a directory path, you can generate the Catagory Path as...

    sub get_catagory_path { my @paths = split /\//, $_[0]; # may need to play with leading / my @catagories; my $working_hash = \@data; foreach $dir ( @paths ) { push @catagories $working_hash->{'main'}; $working_hash = $working_hash->{ $dir }; } return join '/', @catagories; }
    UNTESTED!!!
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: My kingdom for a substring
by DeaconBlues (Monk) on Apr 04, 2001 at 19:00 UTC