Ask a Question related to PERL Beginners, Design and Development.
-
John Baker #1
Referencing Control Operators
Greetings.
Is it possible to reference a control operator?
For example, I'd like to take this code:
sub getFieldFromAllRecords {
my ($self, $directive, $keyword, $matchCondition) = @_;
my ($field, $regArr);
# another public method within same pkg:
my $allRecs = dumpAllAudits($self);
my %ar = %$allRecs;
push @{$regArr}, qr/$keyword/i;
my %select;
while( my($key, $value) = each(%ar)) {
map { next unless ($$value{$directive} =~ /$_/i); } @{$regArr};
$select{$key} = $$value{$directive};
}
return \%select;
}
and change it such that the 'unless' conditional operator is
referenced conceptually similar to the following:
sub getFieldFromAllRecords {
my ($self, $directive, $keyword, $matchCondition) = @_;
my $mc = $matchCondition; # changed: where $mc can be either
# 'if' or 'unless'.
my ($field, $regArr);
# another public method within same pkg:
my $allRecs = dumpAllAudits($self);
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};
}
return \%select;
}
If it's not possible to reference operators, I'll rework the code.
Thanks in advance.
jab
John Baker Guest
-
Referencing a parent control from a child?
I have two UserControls I'd like to have talk to each other. One of them is contained within the other, in a parent/child relationship. The child... -
Referencing Page properties from child control
I'm declaring public properties in a Page's code behind file (not declaratively). I would like to read and assign these from a child control's cs... -
Referencing a TextBox control in Javascript from DataList ItemTemplate
I have a calendar control which pops up as a new window when i click on my textbox. The calendar control returns a Javascript function whereby... -
Referencing Custom Page Class within a Control
C# code. MyPageClass myPage = ( MyPageClass ) this.Page; myPage now is the instance of MyPageClass the control on. bill -
Referencing a control in Javascript
Hi all, In my code-behind, I validate some controls and if a particular control has a problem, I not only send an alert down to the client via... -
Mark Lobue #2
Re: Referencing Control Operators
At 12:12 PM 1/22/2004, John Baker wrote:
Rather than trying to write code into variables or self-modifying code, I think I would try defining some constants:>Greetings.
>
>Is it possible to reference a control operator?
>
>For example, I'd like to take this code:
>
>
> sub getFieldFromAllRecords {
> my ($self, $directive, $keyword, $matchCondition) = @_;
>
> my ($field, $regArr);
>
> # another public method within same pkg:
> my $allRecs = dumpAllAudits($self);
> my %ar = %$allRecs;
>
> push @{$regArr}, qr/$keyword/i;
>
> my %select;
> while( my($key, $value) = each(%ar)) {
> map { next unless ($$value{$directive} =~ /$_/i); } @{$regArr};
> $select{$key} = $$value{$directive};
> }
>
> return \%select;
> }
>
>and change it such that the 'unless' conditional operator is
>referenced conceptually similar to the following:
>
> sub getFieldFromAllRecords {
> my ($self, $directive, $keyword, $matchCondition) = @_;
>
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.
-Mark
> my $mc = $matchCondition; # changed: where $mc can be either
> # 'if' or 'unless'.
> my ($field, $regArr);
>
> # another public method within same pkg:
> my $allRecs = dumpAllAudits($self);
> 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};
> }
>
> return \%select;
> }
>
>If it's not possible to reference operators, I'll rework the code.
>
>Thanks in advance.
>jab
>
>--
>To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
>For additional commands, e-mail: [email]beginners-help@perl.org[/email]
><http://learn.perl.org/> <http://learn.perl.org/first-response>
Mark Lobue Guest
-
Drieux #3
Re: Referencing Control Operators
On Jan 22, 2004, at 4:32 PM, Mark LoBue wrote:[..]> At 12:12 PM 1/22/2004, John Baker wrote:Fashionable and stylish as ever...>>> 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.
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
---
Drieux Guest



Reply With Quote

