in reply to Re: Re: Re: Optimizing quickly hash tables by checking multiple conditions
in thread Optimizing quickly hash tables by checking multiple conditions

my %newhash = map { $_ => $refdes_bom{$_} } grep { CPN($refdes_bom{$_}) and ITEM($refdes_bom{$_}) } keys %refdes_bom;
I have tried to make that basic routine you gave me work but failed as it will not process the keywords bar and foo. The error message I get is :
Undefined subroutine &main::CPN called at compal_test.pl line 37.
Am I missing the obvious basics here ?
  • Comment on Re: Re: Re: Re: Optimizing quickly hash tables by checking multiple conditions
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Optimizing quickly hash tables by checking multiple conditions
by revdiablo (Prior) on May 06, 2004 at 04:50 UTC

    What is in your CPN and ITEM subroutines? You have to define them to check for criteria you want. They don't just magically appear out of nowhere. Here is a fully-functional example, hopefully it will make something click for you:

    my %hash = ( first => { a => 2, b => 3 }, second => { a => 3, b => 2 }, third => { a => 5, b => 4 }, fourth => { a => 4, b => 6 }, ); my %newhash = map { $_ => $hash{$_} } grep { foo($hash{$_}) and bar($hash{$_}) } keys %hash; print "$_\n" foreach keys %newhash; sub foo { my ($href) = @_; $href->{a} > 3; } sub bar { my ($href) = @_; $href->{b} > 2; }

    As you can see, in this example foo makes sure the value at a is greater than 3, and bar makes sure the value at b is greater than 2. The end result should print third and fourth, since those are the only keys that pass the criteria.