Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Map coordinates? by roboticus
in thread Map coordinates? by Anonymous Monk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found