note
roboticus
<p>[Anonymous Monk]:</p>
<p>When you get some incomprehensible code, I'd suggest first running it through perltidy to get a better feel for what it looks like. You can then put in a few print statements to see what the values are initialized to. When I did so, I got this:</p>
<c>
$ perltidy pm_11105453.pl
$ vi pm_11105453.pl
<<< I then used vi to add some print statements to the result >>>
$ cat pm_11105453.pl.tdy
@t = map { $_ * ( $_ + ( $_ - 1 ) ) % 4 } ( 2, 3, 4 );
print "T=(", join(", ", @t), ")\n"; # What do we have in @t?
$i = chr(0x34);
$five = chr(55);
print "I=$i, FIVE=$five\n"; # What's in $i and $five?
print "$i$five "
. ( @t[0] + @t[1] )
. ( $i / 2 ) . "."
. @t[0]
. chr(0x35)
. ( @t[0] * 3 + 1 ) . "\n";
@x = map { ( srand(1) * $_ ) % 9 } ( 28, 34, 57 );
print "X=(", join(", ", @x), ")\n"; # What do we have in @x?
print @x[0]
. ( @x[0] * 2 )
. ( @x[0] * 2 * 2 / 2 ) . " "
. chr(48)
. ( @x[2] * 3 ) . "."
. ( @x[2] + 1 )
. chr(0x30)
. ( ( @x[1] % 4 ) - 3 ) . "\n";
$ perl pm_11105453.pl.tdy
T=(2, 3, 0)
I=4, FIVE=7
47 52.257
X=(1, 7, 3)
122 09.400
</c>
<p>After we run the code, we can see that it sets @t to (2, 3, 0) and @x to (1, 7, 3). If you had <i>real</i> code (instead of this obfuscated nonsense), you could analyze the functions map is applying to the incoming lists for initializing the @t and @x variables. I'd just edit the code and set @t and @x with the final values.</p>
<p>After cleaning up the variable initializations, I'd then start looking for ways to simplify the various bits of code.</p>
<p>For example, $i and $five are nearly unused--$five is used only once in a print statement, $i is used once in an expression and once in a print statement. So I'd delete those two variables and replace their uses with the corresponding values (4 for $i and 7 for $five). Variable deletion isn't always something I suggest, but sometimes it's the right tool.</p>
<p>Next, I'd take advantage of any obvious algebraic simplifications, such as <c>( 4 / 2 )</c> with <c>2</c> and <c>( @x[0] * 2 * 2 / 2 )</c> with <c>( @x[0] * 2 )</c> and also replace constant function calls (such as the ones to chr()) with the results.</p>
<p>Note: You can do a quick command-line experiment to find out the values from constant function calls, like this:</p>
<c>
$ perl -e 'print chr(0x35)'
5
$ perl -e 'print chr(0x30)'
0
$ perl -e 'print chr(48)'
0
</c>
<p>After doing this, and removing the now unnecessary print statements, I got:</p>
<c>
@t = (2, 3, 0);
@x = (1, 7, 3);
print "47 "
. ( @t[0] + @t[1] )
. 2 . "."
. @t[0]
. '5'
. ( @t[0] * 3 + 1 ) . "\n";
print @x[0]
. ( @x[0] * 2 )
. ( @x[0] * 2 ) . " "
. '0'
. ( @x[2] * 3 ) . "."
. ( @x[2] + 1 )
. '0'
. ( ( @x[1] % 4 ) - 3 ) . "\n";
</c>
<p>From here, you could even replace all the references to @t and @x with their values, simplify further and boil the code down to:</p>
<c>
print "47 52.257\n";
print "122 09.400\n";
</c>
<p>...[roboticus]</p>
<p><i>When your only tool is a hammer, all problems look like your thumb.</i></p>
11105453
11105453