http://qs321.pair.com?node_id=11105460


in reply to Map coordinates?

Anonymous Monk:

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:

$ 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

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 real 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.

After cleaning up the variable initializations, I'd then start looking for ways to simplify the various bits of code.

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.

Next, I'd take advantage of any obvious algebraic simplifications, such as ( 4 / 2 ) with 2 and ( @x[0] * 2 * 2 / 2 ) with ( @x[0] * 2 ) and also replace constant function calls (such as the ones to chr()) with the results.

Note: You can do a quick command-line experiment to find out the values from constant function calls, like this:

$ perl -e 'print chr(0x35)' 5 $ perl -e 'print chr(0x30)' 0 $ perl -e 'print chr(48)' 0

After doing this, and removing the now unnecessary print statements, I got:

@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";

From here, you could even replace all the references to @t and @x with their values, simplify further and boil the code down to:

print "47 52.257\n"; print "122 09.400\n";

...roboticus

When your only tool is a hammer, all problems look like your thumb.