http://www.perlmonks.org?node_id=1002649


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.

    +1