Syntactic Confectionery Delight PerlMonks

### A Gear Inch Calculator for Cyclists

by harley_frog (Novice)
 on Aug 12, 2003 at 13:17 UTC ( #283169=CUFP: print w/replies, xml ) Need Help??

Last night, I was doing the exercises in Chapter 2 of The Llama Book and decided to try some "extra credit" coding when I came up with this idea. I used to do a lot of bicycling when I was in college and currently looking to buying a recumbent. With anywhere from 24 to 27 speeds, it's nice to know what the gear inches are for each speed. A gear inch, by the way, is a calculation based on the gear ratios and the size of the drive wheel. It described how "big" your drive wheel is (and in this case bigger means faster). I am hoping to modify the code later to give the entire range of gears, but that's for Chapter 3. ;)

Thoughts, opinions, hints, etc. gladly welcomed.

Frog

```#!/usr/bin/perl -w
print "Enter the number of teeth on the chainring: ";
chomp (\$chainring = <STDIN>);
print "Enter the number of teeth on the cog: ";
chomp (\$cog = <STDIN>);
if (\$cog <= 0) {
print "Error: The gear inches cannot be calculated using \$cog as a c
+og value.  Please enter another value.";
} else {
print "Is your rear wheel metric? (y/n): ";
chomp (\$_ = <STDIN>);
if (\$_ eq "y") {
print "Enter your metric wheel size in millimeters: ";
chomp (\$metric = <STDIN>);
\$wheel = \$metric * .039;
\$gear_inches = (\$chainring / \$cog) * \$wheel;
print "The gear inches for a chainring of \$chainring teeth and a c
+og of \$cog teeth and a wheel of \$metric millimeters is \$gear_inches g
+ear inches.\n";
} else {
print "Enter your wheel size in inches: ";
chomp (\$wheel = <STDIN>);
\$gear_inches = (\$chainring / \$cog) * \$wheel;
print "The gear inches for a chainring of \$chainring teeth and a c
+og of \$cog teeth for a wheel \$wheel inches in diameter is \$gear_inche
+s gear inches.\n";
}
}

Replies are listed 'Best First'.
Re: A Gear Inch Calculator for Cyclists
by Abigail-II (Bishop) on Aug 12, 2003 at 13:59 UTC
Why do beginners programs have the tendency to be overly chatty? Your program prints a line, and read something from input for each chainwheel, whether or not the wheels are metric, and for the size of the wheel.

Could you imagine what life would be if every program acted that way? Just imagine a 'tar' asking you for each option whether you want to include that, and each file to be included to be typed in separatedly? There's a reason many programs take command line arguments, and that programs that read from stdin and write to stdout are often called filters. Programs like yours are much harder to chain together.

I would also perform some form of input checking, and round of the final value.

```#!/usr/bin/perl

use strict;
use warnings;

die "Usage: \$0 <chainring> <cog> <wheelsize>\n" unless @ARGV == 3;

my (\$chainring, \$cog, \$wheelsize) = @ARGV;
\$wheelsize = \$1 * .39 if \$wheelsize =~ /^(\d+)m\$/;

die "Chainring needs to be a positive integer\n" unless \$chainring =~
+/^\d+\$/;
die "Cog needs to be a positive integer\n"       unless \$cog =~ /^\d+\$
+/;
die "Wheelsize needs to be a positive number\n"  unless
\$wheelsize =~ /^\d+(?:[.]\d*)?\$/;

my \$gearinches = sprintf "%.2f" => \$wheelsize * \$chainring / \$cog;

print <<"--";
The gear inches for a chainring of \$chainring teeth and a
cog of \$cog teeth, and a wheel of \$wheelsize inches is
\$gearinches gear inches.
--

__END__

Abigail

Why do beginners programs have the tendency to be overly chatty?

I think it has a lot to do with insecurity. Beginners want to be continually assured that the program is actually doing what they think they told the program to do.

Once you are more experienced, you are self assured about your programming capabilities, and you "know" that the program is doing what you told it to do. So you don't need the continuous assurances anymore.

Liz

Re: A Gear Inch Calculator for Cyclists
by CountZero (Bishop) on Aug 13, 2003 at 06:19 UTC

To cater for a user who types 'Y' instead of 'y', it is considered better to normalize the user input to the same case as your test (unless you want the program to react differently between 'Y' and 'y'):

```print "Is your rear wheel metric? (y/n): ";
chomp (\$_ = <STDIN>);
if (lc(\$_) eq "y")

As lc by default works on \$_, you could use lc instead of lc(\$_), but that is perhaps unnecessarily obfuscated (Are there necessary obfuscations?)

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Create A New User
Node Status?
node history
Node Type: CUFP [id://283169]
Approved by phydeauxarff
help
Chatterbox?
 [Discipulus]: i fear no [Discipulus]: i invented also 'gired' [Lady_Aleena]: Discipulus, then that is a problem. I wanted to find total seconds of my entire .mp3 collection to do some math on it to see how many days of continuous music i have. [LanX]: darn. .. I wanted to see Marine and Melonchon go to next round, just for fun xD [Discipulus]: LA use Perl: traversing a dir tree is simple; foreach file mp3 \$sec+= use Mp3::Info [Lady_Aleena]: Discipulus, I was able to get the sum of the seconds on the command line using awk. Then I ran the result through a perl script and got the result. It came bach with 4 days, 9 hours, 56 minutes, 43 seconds. [Lady_Aleena]: s/bach/back/; [Discipulus]: gired anyway is the intermediate state between hired and fired (and vice versa;) [Lady_Aleena]: Discipuluis, I don't know if I want to fight with File::Find. File::Find and I are not friends. [Discipulus]: 4 day Bach is too muche even for me

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2017-04-23 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I'm a fool:

Results (432 votes). Check out past polls.