<?xml version="1.0" encoding="windows-1252"?>
<node id="82164" title="Re: Re: (Golf) Reversing RPN Notation" created="2001-05-22 05:38:53" updated="2005-07-19 14:08:39">
<type id="11">
note</type>
<author id="17251">
MeowChow</author>
<data>
<field name="doctext">
You have the right idea. Perhaps this will help you get started:
&lt;code&gt;
sub rpn_ptree {
  my@s;$o{$_}?$s[--$#s]=[$_,@s[-2,-1]]:push@s,$_ for@_;@s
}
&lt;/code&gt;
It's a pretty well-golfed sub that builds a parse tree out of an RPN expression. The only tricky part is the &lt;tt&gt;--$#s&lt;/tt&gt;, which simultaneously pops off the top element of &lt;tt&gt;@s&lt;/tt&gt; (by assigning to $#s), and returns the index of the new end of the array. And here's another way to build a parse tree:
&lt;code&gt;
sub rpn_ptree {
  my$i;$o{$_[$i]}&amp;&amp;splice@_,$i-=2,3,[@_[$i..$i+2]]while++$i&lt;@_;@_
}
&lt;/code&gt;
&lt;pre&gt;
  &lt;a href="/index.pl?node=MeowChow"&gt; MeowChow                                   &lt;/a&gt;
               s aamecha.s a..a\u$&amp;owag.print&lt;/pre&gt;</field>
<field name="root_node">
81969</field>
<field name="parent_node">
82139</field>
</data>
</node>
