develooper Front page | perl.beginners | Postings from April 2010

Re: matching values of one hash to another

Thread Previous | Thread Next
From:
John W. Krahn
Date:
April 29, 2010 11:29
Subject:
Re: matching values of one hash to another
Message ID:
4BD9D016.8010100@shaw.ca
Harry Putnam wrote:
> 
> I posted my effort at digging out the matches.
>    
> I see the way you did it, even though not the exact results I was
> after is 100s of percent better way to write it.
> 
> I'm curious though if the overhead is different in your compact code
> compared to mine. That is, if all that spinning through dir2:
> ,----
> |   my ($r1full,$r1end);
> |  while (($r1full,$r1end) = each(%r1h)) {
> |    foreach my $key (keys %r2h) {
> `----
> 
>  is more costly than your compact example: 
> 
> ,----
> |     if ( exists $r2h{ $rlend } ) {
> |        print "$r2h{$rlend} MATCHES $r1end\n";
> `----

Yes.

> In other words is the perl interpreter working harder in one case?

Yes.


> [...]
> 
> cat ex1.pl
> #!/usr/local/bin/perl
> 
> use strict;
> use warnings;
> use File::Find;
> use Cwd;
> 
> my $topdir2recurse = shift;
> 
> ## Trying to use JKs' notation
> 
> my $cnt1 = 0;
> find  sub {
>         if(-f $File::Find::name){
>           $cnt1++;
>         }
>       },$topdir2recurse;
> 
> print "Finished 1st recurse, count was <$cnt1>\n";
> # -------       -------       ---=---       -------      ------- 
> my $cnt2 = 0;
> find  sub {
>        my $dir = getcwd;
>        if(-f $dir . '/' . $_){
>           $cnt2++;
>        }
>       },$topdir2recurse;
> 
> print "Finished 2nd recurse, count was <$cnt2>\n";
> 
>             __END__
> -------        ---------       ---=---       ---------      -------- 
> run ex1.pl ./dir1
>   Finished 1st recurse, count was <0>
>   Finished 2nd recurse, count was <625>
> -------        ---------       ---=---       ---------      -------- 
> I'm sure my test is invalid since you've shown that $File::Find:;dir
> is all I needed.  But not sure I see why it works in one case but not
> this case.  (Something wrong with the example no doubt)

You need to do the -f file test on $_ instead of either 
$File::Find::name or getcwd() . '/' . $_ because *you* *are* *using* 
*relative* *paths* instead of absolute paths.  File::Find::find() 
already changes your location to $File::Find::dir so $_ is *in* *the* 
*current* *directory* and there is no need to prepend anything to $_ to 
use a file test on it.  Trying to use a file test on $File::Find::name 
searches for the file name *relative* to the current directory.


> [...] script snipped
> 
> Yes, once I get it working... and figure out what is actually going on
> in that compact code it is the kind of code I'd like to be able to
> dash off (and read) some day.
> 
> I don't see (yet) what is supposed to be happening here.  There is a
> lot happening that isn't obvious to me.
> 
>        $data{ $_ }{ $r2 }++
>    $data{ $_ } .. (ok thats what I called the end name)
>  
> Are {} playing the role of rgx delimiters in `{ $r2 }'

What are "rgx" delimiters?

> Or is it the same as saying:
>    $data{ $_ } eq $r2

No.

> I'm not sure what roll the `++' plays there either.    

That is incrementing the value in $data{ $_ }{ $r2 }.




John
-- 
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About