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

Re: Pattern Matching Query

by thunders (Priest)
on Sep 17, 2002 at 20:50 UTC ( [id://198634]=note: print w/replies, xml ) Need Help??


in reply to Pattern Matching Query

similar to some of the above and limited to only the cases you mentioned
if ('abcdefghijklm' =~ qr/$sv/ && $sv =~/^[a-m]{5}$/){ &do_stuff }

Replies are listed 'Best First'.
Re: Re: Pattern Matching Query
by PhiRatE (Monk) on Sep 18, 2002 at 05:25 UTC
    Good implementation, I consider it the best of those posted because it explicitly matches only those conditions that were given in the example as you say, as opposed to some of the others which match considerably more. With poker, you don't really want to screw up :)

    I offer a slight modification:

    if (join('','a'..'m') =~ qr/$sv/ && $sv =~/^[a-m]{5}$/){ print "Matched\n"; }

    Which just means you don't need to specify the whole of the range, just the start and end, you could sub it like this:

    sub match_range { my ($string, $start, $end, $length) = @_; if (join('',$start .. $end) !~ qr/$string/) { return 0; } if ($string !~ /^[${start}-${end}]{$length}$/) { return 0; } return 1; }
      Nice, but since boolean operators shortcircuit anyway (ie the right operand of && won't even be evaluated if the left one is already false), you just added a lot of blocks for no gain. Also, return 0 is a bad meme, if you want to return false then just return (which is equivalent to return wantarray ? () : undef).
      sub match_range { my ($string, $start, $end, $length) = @_; my $range = join '', $start .. $end; return $range =~ /$string/ && $string =~ /^[$range]{$length}$/; }
      Just a bit of nitpickery.. :-)

      Makeshifts last the longest.

        Actually what I did was introduce two guard clauses. I am aware that it was correct in either fashion, it is my preference to have drop-through rules of the manner displayed previously. I also prefer explicit "false" return, and if there was some way I could say "return false" and get the same effect as just "return" I'd be using it :) undef isn't a bad concept tho.

        Those are simply elements of my style however, the reasoning behind guard clauses is obvious in larger functions, in this case it is superfluous but assuming a reasonably efficient action on the part of the interpreter I see no likely efficiency loss, so the consistency rule applies :)

        The re-use of the $range var is nice however.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-18 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found