>>>>> "HP" == Harry Putnam <rea...@newsguy.com> writes:
HP> I need to do some matching of filenames in two top level directories. HP> We expect to find a number of cases where the endnames ($_) are the HP> same in both hierarchies but the full name is different. someone else mentioned this but endname and fullname are not the best way to say those things. there is the filename, the directory (optionally with path) and the full path. HP> base1/my/file HP> base2/my/different_path/file so what are you looking for? please state the goal and don't assume we can read your mind just from your code. are those 'matched' or mismatched? HP> I've made hashes of the file names in two top level directories: HP> I've assembled the hashes using File::Find. We sort them first by HP> matching the base directory to the top directories passed in. huh? that isn't sorting if i understand what you said. maybe organizing or something else. HP> My first impulse wast to do it by matching the endnames (or value) in HP> one hash to any matching endname (value) in the other. There are HP> several more actions that follow (not coded yet) but That's a lot of HP> spinning... what if you have duplicate FILENAMES in one tree? or dups in one tree and one in the other? again, learn to state your goals first. comparing two trees can be done with many different goals and techniques to achieved those goals. HP> I'm thinking there are better ways to do that. So .. the script below HP> does it the hard way, with all that spinning. Can anyone suggest HP> another way? HP> ------- --------- ---=--- --------- -------- HP> #!/usr/local/bin/perl HP> use strict; HP> use warnings; HP> use File::Find; HP> use Cwd; HP> my $r1 = shift; HP> my $r2 = shift; HP> my %r1h; HP> my %r2h; don't use short names like that. r1 tells me nothing about the data or its usage. HP> my $r1hkcn = 0; HP> my $r2hkcn = 0; HP> find( HP> sub { HP> ## For use in guaranteeing the -f command uses the HP> ## right path HP> my $dir = getcwd; HP> if (-f $dir . '/'. $_) { "$dir/$_" HP> ## Determine if base dir matches r1 or r2 HP> (my $base) = $File::Find::name =~ m/^(\.*\/*\/[^\/]+)\//; blech! use an alternate delimiter if / is in the regex. that is leaning toothpicks. and m/ isn't needed IF the delim is / to begin with. my ($base) = $File::Find::name =~ m{^(\.*/*/[^/]+)/} ; even better for crazy regexes like that use /x and break it up over lines with comments about what you are looking for. or use a module which has a basename func. HP> if ($r1 eq $base) { HP> $r1h{$File::Find::name} = $_; HP> $r1hkcn++; i have no idea what that counter is doing because the name is noise. names should reflect usage and not be some nmemonic for you. HP> if ($r1hkcn == 1) { HP> print "v:$File::Find::name k:$_\n"; HP> } HP> } else { HP> $r2h{$File::Find::name} = $_; HP> $r2hkcn++; HP> if ($r2hkcn == 1) { HP> print "v:$File::Find::name k:$_\n"; HP> } HP> } HP> } HP> }, HP> $r1,$r2 use explict return statements. one day someone will add a return statement and that last expression will not be returned. worse things can happen too. HP> ); HP> my ($r1full,$r1end); HP> while (($r1full,$r1end) = each(%r1h)) { combine those two lines and use whitespace. drop excess parens too in this case (too noisy) : while ( my( $r1full, $r1end ) = each %r1h ) { i can't comment on the overall program since as i said i can't tell what your goal is. your code and weak names don't make it easy for me to discern what you are thinking. uri -- Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/