Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Flag variables

by KPeter0314 (Deacon)
on Mar 03, 2003 at 15:35 UTC ( [id://240056]=note: print w/replies, xml ) Need Help??


in reply to Flag variables

Not to be a troublesome monk, but I have a twist on the Wisdom that oakbox seeks. I too being just a learning monk with nobody local to learn from.

Given that the code in the original post is not what we should aspire to, what about looking for multiple matching items in the array? Say for instance that the array is not unique and you want to look for an instance of "foo" and also "bar". Either of these may show up once, many times, or even never. In any case we only wan to run the resulting action once

Would the code from blakem be the best? I hadn't used the grep fuction in this way and am glad to be illumined to it. It would seem to be expensive though if @somearray was rather large to iterate through it multiple times.

fooaction() if grep { $_ eq "foo" } @somearray; baraction() if grep { $_ eq "bar" } @somearray;
Or, would there be a better way than expanding the code to this if the @somearray were large:
my $fooflag = 0; my $barflag = 0; foreach my $var (@somearray){ if($var eq "foo"){ $fooflag = 1;} if($var eq "bar"){ $barflag = 1;} } if($fooflag eq "1"){ &fooaction;} if($barflag eq "1"){ &baraction;}
Note: you wouldn't want to jump out with a "last" as that would prevent you from checking for other conditions.

-Kurt

Replies are listed 'Best First'.
Re: Re: Flag variables
by BrowserUk (Patriarch) on Mar 03, 2003 at 16:37 UTC

    What this code is doing is building a delayed action dispatch table. Whenever you need to look a variable up in an array, you should consider using a hash. That is exactly their forte.

    my %lookup = ( foo=>\&fooaction, bar=>\&baraction, qux=>\&quxaction ); if (exists $lookup{$var}) { $lookup{$var}->(); } else { #do the default or exception handling here. }

    It cleaner and clearer to read, uses loads less variables, is much easier to maintain and extend and more efficient to boot.

    If brevity is compatible with your mindset, then the if/else can become

    &{ $lookup{$var} || $default_subref }->( parms );

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      Wow! KPeter0314 bows to a Saint

      Looked at this for a couple moments before it really hit me what was happening. I use hashes regularly in my code but never quite like this. Brilliant and slick. I expect a hash lookup is going to be quite efficient. Especially since this turns the problem on its ear and does a hash lookup of the value to a small result set.

      Using the hash reference to return the sub name that you end up executing is my favorite part. (now isn't that a sentence)

      -Kurt

        Using the hash reference to return the sub name...

        It's worth pointing out that it doesn't actually return the name of the sub, but rather a coderef for the sub.

        Whilst you could name the subs with the same name as the value used to look them up, and then avoid the need to use the hash at all, that would be an example of using symbolic references with all the inherent dangers that implies.

        By storing and looking up a coderef to the action subroutines, you avoid the possibility of attempting to invoke a non-existant subroutine if $var has an unexpected value and more importantly, avoid the possibility of invoking one of the potentially dangerous built-ins if the contents of $var has been deliberately tampered with.


        Examine what is said, not who speaks.
        1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
        2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
        3) Any sufficiently advanced technology is indistinguishable from magic.
        Arthur C. Clarke.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://240056]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-18 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found