in reply to Complicated Search and Replace

What follows is a recursive solution. The expressions are paired, then the pairs are paired, then the paired pairs are paired, etc. until only one expression is left.

use strict; use warnings; my $command = '@MAC(output,X,Y,8);'; my ($fields) = $command =~ /\@MAC\((.*?)\)/; my ($output, $var1, $var2, $size) = split(/\s*,\s*/, $fields); my @data = map { "$var1$_*$var2$_"} (0 .. $size-1); while (@data > 1) { my @compressed; push @compressed, sprintf("(%s)+(%s)", shift(@data), shift(@data)) while @data; @data = @compressed; } print("$output = $data[0];\n");

Replies are listed 'Best First'.
Re^2: Complicated Search and Replace
by gantlord (Initiate) on Jun 27, 2005 at 18:43 UTC
    Wow. That was quick. I was trawling through the man pages trying to come up with something to post so I didn't look like such a freeloader... Your solution works. Try putting in 8192 and you'll appreciate how much time is to be saved!

    Thanks
      Try putting in 8192 and you'll appreciate how much time is to be saved!
      >time < nul & echo. & perl !.pl > output & time < nul & echo. The current time is: 14:46:18.10 Enter the new time: The current time is: 14:46:18.76 Enter the new time:

      I'd say! :)

      output = (((((((((((((X0*Y0)+(X1*Y1))+((X2*Y2)+(X3*Y3)))+...