On Jan 22, 2004, at 4:32 PM, Mark LoBue wrote:
At 12:12 PM 1/22/2004, John Baker wrote:
[..]
and change it such that the 'unless' conditional operator is
referenced conceptually similar to the following:

  sub getFieldFromAllRecords {
  my ($self, $directive, $keyword, $matchCondition) = @_;


Rather than trying to write code into variables or self-modifying code, I think I would try defining some constants:


use constant MATCH_IF => 0;
use constant MATCH_UNLESS => 1;

Then assign $mc to MATCH_IF or MATCH_UNLESS any way you like

Then use xor:

map { next if ( ($$value{$directive} =~ /$_/i) xor $mc); } @{$regArr};

This is untested, but you get the idea.

Fashionable and stylish as ever...


But it has the problem that it will cause a runtime error.
The following I think may help the OP:

        use constant MATCH_IF => 0;
        use constant MATCH_UNLESS => 1;

        my @list = qw/foo Foo bar baz/;
        
        my @foo = if_unless('foo',MATCH_IF,@list);
        
        my @not_foo = if_unless('foo',MATCH_UNLESS,@list);
        
        print "foo got $_\n" foreach(@foo);
        print "not_foo got $_\n" foreach(@not_foo);
        
        #------------------------
        #
        sub if_unless
        {
                my ($token, $mc, @list) = @_;
                
                grep { $_ if ( ($token =~ /$_/i) xor $mc) } @list;
                
        } # end of if_unless

generates:

foo got foo
foo got Foo
not_foo got bar
not_foo got baz

whereas changing the 'grep' to a 'map' will generate:

foo got foo
foo got Foo
foo got
foo got
not_foo got
not_foo got
not_foo got bar
not_foo got baz

so there is that point to take into account as to what one is
really trying to do here.

[..]
my %ar = %$allRecs;

push @{$regArr}, qr/$keyword/i;

my %select;
while( my($key, $value) = each(%ar)) {
map { next $mc ($$value{$directive} =~ /$_/i); } # changed
@{$regArr};
$select{$key} = $$value{$directive};
}
[..]

now the minor question, why assign the %ar? you could
ahve done the while

while( my($key, $value) = each %$allRecs) {

minor nit.

So what exactly were you planning to assign the output
of that map to???

ciao
drieux

---


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to