Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

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

The main problem isn't finding $mod, it's getting it to work, and you've done that. Congratulations!

However, there are a couple of things you might find it useful to change.

Firstly, "use strict" at the top of your script. Always. Until you know enough to ignore that advice.

Secondly, try having a short main body of code calling a couple of top-level functions:

#!/usr/bin/perl ... my @polygon = build_polygon($num_points, $radius); print_svg_polygon(@polygon); exit; sub build_polygon { ...

It makes it easy to see what the script does at a glance, and lets you re-use bits more easily.

Thirdly, make sure you scope your variables. Use "my" to restrict them to their defining block. Otherwise, the $n in calcMagic for example is the same as the $n at the top of your script. If you changed it inside the sub it would change in the main script too.

sub calcMagic { my ($n) = @_; my $m = -1; my $mod = 1; for(1..$n) { $mod += $m; $m = nextMagic($m); } return $mod; }

Finally, read up on Getopt::Long (http://search.cpan.org/~jv/Getopt-Long-2.38/lib/Getopt/Long.pm). That will let you call add named options to your script so you can do something like:

draw_star.pl --points=5 --radius=200

It's a well-known and well tested module, and has good documentation.

You could also replace the multiple "if" statements with a lookup into an array, but I'm not sure it would make the code any clearer really.

Hope that's helpful.


In reply to Re: RFC: Creating unicursal stars by huxtonr
in thread RFC: Creating unicursal stars by Fox

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 pondering the Monastery: (5)
As of 2024-04-19 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found