for(<>){ $i=1;@m=(); while(/\S /||$i--){ s/^(\d\S*) (\d\S*)/"$1$2".(pop@m)/e and s/^/(pop@m)." "/e and redo; s/(\S+) // and push@m,$1; }s/./$& /g;s/^ +| $//g; print }