Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Perl Parsing Based on Supplied Precedence

by grizzley (Chaplain)
on Nov 07, 2012 at 10:01 UTC ( #1002649=note: print w/replies, xml ) Need Help??

in reply to Perl Parsing Based on Supplied Precedence

You can simplify code a little bit by moving $node declaration to the inner loop (and I replaced condition $3 with match result):
sub parse { my ($regex,$input)=@_; for(@$regex) { if($input=~m/(.+)($_)(.+)/) { my ($before,$op,$after,$node)=($1,$2,$3); $node->{$op}=[parse($regex,$before), parse($regex,$after)] +; return $node; } } return $input; }
and optimize the code by moving $input=~s/\s//g outside parse sub. It is enough to do this once in while loop after reading input data.

Replies are listed 'Best First'.
Re^2: Perl Parsing Based on Supplied Precedence
by protist (Monk) on Nov 07, 2012 at 11:41 UTC

    That is very slick. I was worried about checking for a match then doing a match with groups being two regex operations, but for some reason didn't think to do both at once as you did.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002649]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2018-06-18 06:45 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.