Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Tales from writing a RPN evaluator in Perl 5, Perl 6 and Haskell

by ambrus (Abbot)
on Jan 04, 2006 at 22:20 UTC ( #521037=note: print w/ replies, xml ) Need Help??


in reply to Tales from writing a RPN evaluator in Perl 5, Perl 6 and Haskell

$tok =~ /-?\d+/ only checks whether the token contains an integer, so the token can be an integer and some junk before and after it. If I were you, I'd either use anchors like $tok =~ /^-?\d+$/ or at least use only the integer-looking part: if ($tok =~ /(-?\d+)/) { push @stack, $1; ...


Comment on Re: Tales from writing a RPN evaluator in Perl 5, Perl 6 and Haskell
Select or Download Code
Re^2: Tales from writing a RPN evaluator in Perl 5, Perl 6 and Haskell
by Anonymous Monk on Jan 04, 2006 at 23:45 UTC
    TIMTOWTDI...
    $x = "12plus some garbage"; $y = "5+-*&^%$#@!"; $sixty = $x * $y; print "$sixty\n"

      Yes, but not for strings like "12.99999e-2" or "somejunk42".

Re^2: Tales from writing a RPN evaluator in Perl 5, Perl 6 and Haskell
by eyepopslikeamosquito (Canon) on Jan 05, 2006 at 02:36 UTC

    You are right. My intent was to use /^-?\d+$/ but somehow I "forgot" to insert the anchors. :-( Curiously, when emulating the regex in hand-rolled Haskell code, I somehow "remembered" to insert the anchors. :-) I've updated the Perl code in the root node by inserting the anchors. The Haskell isStrDigit function already checks for all digits (equivalent to using the anchors) and so does not require update. Thanks ambrus.

      This sort of mistake is one of the reasons we're thinking about distinguishing two different levels of pattern match in Perl 6, which we're currently calling "token" matching and "rule" matching, for lack of better names. Rule matching does scanning while token matching doesn't. When a token is matched from within a rule it has to match at the current location, and it's the embedding of the token within the rule match that allows other stuff to match after the token. By itself, a token must match an entire string (maybe with surrounding whitespace). So eventually we should have a way to just ask if the string matches a "num" or "int" pattern of some sort by pretending the token matcher is a subroutine or method of some sort. Perhaps somewhere in the relationship between tokens and rules we also can manage whitespace without scattering modifiers everywhere.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-12-28 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (181 votes), past polls