"which I will try to build & use nested loops in, when I can get it functioning"
Exactly the wrong way around! Think structure (overall program structure and data structure) first, then details. By the time you've written the details it is far too late to go back and rethink the structures.
For complicated projects it's often said "write the first version to throw away". It's only when the first version has been written that you have an idea of how you perhaps should have written it. Getting a better first cut is mostly a matter of experience.
A major red flag (code smell/stink) is variable names with index numbers in them. Whenever you find yourself doing that, change to using an array. For your current task however that just isn't an issue because you don't need to store the intermediate values at all. Consider:
#!/usr/bin/perl
use strict;
use warnings;
my $wheel = 26.5;
my @chainWheels = qw (42 32 22);
my @cogs = qw (11 13 15 17 19 21 24 28 32);
print "Wheelsize: $wheel\n";
print "Chainwheels: @chainWheels\n";
print "Sprockets: @cogs\n\n";
for my $chainSel (1 .. @chainWheels) {
my $wheelMul = $wheel * $chainWheels[$chainSel - 1];
print "Chain wheel: $chainSel ratios: \n";
for my $cogSel (1 .. @cogs) {
printf "%d: %5.2f ", $cogSel, $wheelMul / $cogs[$cogSel - 1];
}
print "\n";
}
Prints:
Wheelsize: 26.5
Chainwheels: 42 32 22
Sprockets: 11 13 15 17 19 21 24 28 32
Chain wheel: 1 ratios:
1: 101.18 2: 85.62 3: 74.20 4: 65.47 5: 58.58 6: 53.00 7: 46.38 8: 39.
+75 9: 34.78
Chain wheel: 2 ratios:
1: 77.09 2: 65.23 3: 56.53 4: 49.88 5: 44.63 6: 40.38 7: 35.33 8: 30.2
+9 9: 26.50
Chain wheel: 3 ratios:
1: 53.00 2: 44.85 3: 38.87 4: 34.29 5: 30.68 6: 27.76 7: 24.29 8: 20.8
+2 9: 18.22
True laziness is hard work
-
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.
|