But you also have to correct your calculations section to deal with the division by zero problem: you have nine tooth-counts (elements) in @chwhs but, as written, you use only three, $chwhs[0], $chwhs[1] and $chwhs[2] while attempting to use 9 elements of @cogs which has only three elements, 42, 32 and 22.
Without too much diddling your code, you can fix that problem, thusly:
#!/usr/bin/perl use 5.014; # 9913950mod print "\nMulti Gear Calculator\n\n"; print "\ Enter Wheelsize (inches): "; my $wheel = <STDIN>; chomp $wheel; print "\n Enter nine numeric values for Chainwheel Teeth: "; # 11 13 1 +5 17 19 21 24 28 32 my $chwhs = <STDIN>; chomp $chwhs; my @chwhs = split (/\s+/, $chwhs); chomp @chwhs; print "\n Enter three numeric values for Cog Teeth: "; # 42 33 22 my $cogs = <STDIN>; chomp $cogs; say "\t $cogs \n\n"; # DEMO: SEE co +mment in output my @cogwheels = split (/\s+/, $cogs); chomp @cogwheels; my $gear1 = $wheel * $chwhs[0]/$cogwheels[0]; my $gear2 = $wheel * $chwhs[1]/$cogwheels[0]; my $gear3 = $wheel * $chwhs[2]/$cogwheels[0]; my $gear4 = $wheel * $chwhs[3]/$cogwheels[0]; my $gear5 = $wheel * $chwhs[4]/$cogwheels[0]; my $gear6 = $wheel * $chwhs[5]/$cogwheels[0]; my $gear7 = $wheel * $chwhs[6]/$cogwheels[0]; my $gear8 = $wheel * $chwhs[7]/$cogwheels[0]; my $gear9 = $wheel * $chwhs[8]/$cogwheels[0]; my $gear10 = $wheel * $chwhs[0]/$cogwheels[1]; my $gear11 = $wheel * $chwhs[1]/$cogwheels[1]; my $gear12 = $wheel * $chwhs[2]/$cogwheels[1]; my $gear13 = $wheel * $chwhs[3]/$cogwheels[1]; my $gear14 = $wheel * $chwhs[5]/$cogwheels[1]; my $gear15 = $wheel * $chwhs[4]/$cogwheels[1]; my $gear16 = $wheel * $chwhs[5]/$cogwheels[1]; my $gear17 = $wheel * $chwhs[6]/$cogwheels[1]; my $gear18 = $wheel * $chwhs[7]/$cogwheels[1]; my $gear19 = $wheel * $chwhs[0]/$cogwheels[2]; my $gear20 = $wheel * $chwhs[1]/$cogwheels[2]; my $gear21 = $wheel * $chwhs[2]/$cogwheels[2]; my $gear22 = $wheel * $chwhs[3]/$cogwheels[2]; my $gear23 = $wheel * $chwhs[4]/$cogwheels[2]; my $gear24 = $wheel * $chwhs[5]/$cogwheels[2]; my $gear25 = $wheel * $chwhs[6]/$cogwheels[2]; my $gear26 = $wheel * $chwhs[7]/$cogwheels[2]; my $gear27 = $wheel * $chwhs[8]/$cogwheels[2]; my @gear1 = ($gear1, $gear2, $gear3, $gear4, $gear5, $gear6, $gear7, $ +gear8, $gear9); my @gear2 = ($gear10, $gear11, $gear12, $gear13, $gear14, $gear15, $ge +ar16, $gear17, $gear18); my @gear3 = ($gear19, $gear20, $gear21, $gear22, $gear23, $gear24, $ge +ar25, $gear26, $gear27); my $rounded; # re-used throughout the output funcs below print "\n Your gear ratios are: \n"; print "\n Sprocket 1: "; for (@gear1) { $rounded = sprintf("%.3f", $_); print $rounded . " | "; } print "\n Sprocket 2: "; for (@gear2) { $rounded = sprintf("%.3f", $_); print $rounded . " | "; } print "\n Sprocket 3: "; for (@gear3) { $rounded = sprintf("%.3f", $_); print $rounded . " | "; } print "\nWheelsize: $wheel \n"; print "Chainwheels: @chwhs \n"; print "Cogteeth: @cogwheels \n\n"; =head execution (annotate): C:\>991395OPmod.pl Multi Gear Calculator Enter Wheelsize (inches): 26.5 Enter nine numeric values for Chainwheel Teeth: 11 13 15 17 19 21 24 +28 32 Enter three numeric values for Cog Teeth: 42 32 22 42 32 22 # Perl sees the three values from <STDIN> as a SING +LE, non-numeric string # that's why we need to split them, to get three nu +meric values Your gear ratios are: Sprocket 1: 6.940 | 8.202 | 9.464 | 10.726 | 11.988 | 13.250 | 15.143 + | 17.667 | 20.190 | Sprocket 2: 9.109 | 10.766 | 12.422 | 14.078 | 17.391 | 15.734 | 17.3 +91 | 19.875 | 23.188 | Sprocket 3: 13.250 | 15.659 | 18.068 | 20.477 | 22.886 | 25.295 | 28. +909 | 33.727 | 38.545 | Wheelsize: 26.5 Chainwheels: 11 13 15 17 19 21 24 28 32 Cogteeth: 42 32 22 =cut
philiprbrenan incorporates that necessity, but -- unfortunately for the clarity of his example -- ...
Modifying his code to use your data and design,
#!/usr/bin/perl use 5.014; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); # 9913950PB my $wheel = 26.5; my @chwhs = qw (11 13 15 17 19 21 24 28 32); # test data my @cogs = qw (42 32 22); # test data my $gear; for my $c(0..8) { $gear->[$c][$_] = $wheel * $chwhs[$c]/$cogs[$_] for 0..2; } pp($gear);
one obtains output -- formatted differently -- but similar to the above:
[ [6.94047619047619, 9.109375, 13.25], [8.20238095238095, 10.765625, 15.6590909090909] [9.46428571428571, 12.421875, 18.0681818181818] [10.7261904761905, 14.078125, 20.4772727272727] [11.9880952380952, 15.734375, 22.8863636363636] [13.25, 17.390625, 25.2954545454545], [15.1428571428571, 19.875, 28.9090909090909], [17.6666666666667, 23.1875, 33.7272727272727], [20.1904761904762, 26.5, 38.5454545454545], ]
update: Note, however, that philiprbrenan's code DOES exhibit the virtue of succinctness while Athanasius' use of a data section and the easy mod from my $fh = \*DATA; to my $fh = \*STDIN; has great merit -- especially for a newcomer who may have to interate test runs, ad nauseum.
and... interspersed updates to fix my formatting in this overlong observation... BUT, I'm still not sure why the ratio values are interspersed rather than sequential across their entire span from gear1 to gear27.
In reply to Re: I'm trying to get a numeric array
by ww
in thread I'm trying to get a numeric array
by fatmac
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |