Professional Web Applications Themes

Referencing Control Operators - PERL Beginners

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 ...

  1. #1

    Default 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

  2. #2

    Default Re: Referencing Control Operators

    At 12:12 PM 1/22/2004, John Baker wrote:
    >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) = _;
    >
    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.

    -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-unsubscribeperl.org[/email]
    >For additional commands, e-mail: [email]beginners-helpperl.org[/email]
    ><http://learn.perl.org/> <http://learn.perl.org/first-response>

    Mark Lobue Guest

  3. #3

    Default 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:
    [..]
    >> 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

    ---

    Drieux Guest

Similar Threads

  1. Referencing a parent control from a child?
    By Steve Hershoff in forum ASP.NET Building Controls
    Replies: 4
    Last Post: July 31st, 05:25 PM
  2. Referencing Page properties from child control
    By in forum ASP.NET Building Controls
    Replies: 5
    Last Post: August 8th, 11:57 PM
  3. Referencing a TextBox control in Javascript from DataList ItemTemplate
    By Candice in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: December 5th, 07:11 AM
  4. Referencing Custom Page Class within a Control
    By William F. Robertson, Jr. in forum ASP.NET General
    Replies: 1
    Last Post: July 10th, 08:54 PM
  5. Referencing a control in Javascript
    By John in forum ASP.NET General
    Replies: 2
    Last Post: July 8th, 12:08 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139