Here's another solution:

use strict; use warnings; while ( <DATA> ) { my ( $args ) = /\@MAC\((.*)\)/; my ( $lhs, $v0, $v1, $e ) = split /,/, $args; print "$lhs=\n", term( $v0, $v1, [0..$e-1] ), "\n"; } sub term { my $indices = pop; my $n = $#$indices; if ( $n ) { my $m = int($n/2); my @t = map term( @_, $_ ), [ @{$indices}[0..$m] ], [ @{$indices}[$m+1..$n] ]; return "($t[0])+($t[1])"; } else { my ( $v0, $v1 ) = @_; my $first = $indices->[0]; return "$v0$first*$v1$first"; } } __DATA__ @MAC(Result,A,B,2) @MAC(output,X,Y,8) __END__ Result= (A0*B0)+(A1*B1) output= (((X0*Y0)+(X1*Y1))+((X2*Y2)+(X3*Y3)))+(((X4*Y4)+(X5*Y5))+((X6*Y6)+(X7* +Y7)))

the lowliest monk


In reply to Re: Complicated Search and Replace by tlm
in thread Complicated Search and Replace by gantlord

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.