Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: I'm trying to get a numeric array

by GrandFather (Cardinal)
on Sep 03, 2012 at 22:06 UTC ( #991518=note: print w/ replies, xml ) Need Help??


in reply to I'm trying to get a numeric array

"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


Comment on Re: I'm trying to get a numeric array
Select or Download Code
Re^2: I'm trying to get a numeric array
by fatmac (Novice) on Sep 04, 2012 at 08:11 UTC
    Thankyou very much for your reply GrandFather; nested loops was to be my next challenge, & you have provided me with an excellent example.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991518]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (16)
As of 2014-04-17 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (450 votes), past polls