
davorg
Thaumaturge
/ Moderator
Nov 4, 2004, 2:20 AM
Views: 4102
|
|
Re: [ShipOfFools] convert Farenheit to Celcius
|
|
|
I wrote it like that to be as close as possible to the original code. If you're interested in how I'd actually write the program, it would probably be more like this:
#!/usr/bin/perl use strict; use warnings; no warnings 'numeric'; my %subs = ( F => sub { ($_[0] - 32) * 5 / 9 }, C => sub { ($_[0] * 9 / 5) + 32 } ); my %scale = ( F => 'C', C => 'F' ); if ($ARGV[0] =~ /([cf])\s*$/i and $subs{uc $1}) { print "$ARGV[0] is ", $subs{uc $1}->($ARGV[0]), "$scale{uc $1}\n"; } else { print "Invalid input: @ARGV\n"; } -- Dave Cross, Perl Hacker, Trainer and Writer http://www.dave.org.uk/ Get more help at Perl Monks
(This post was edited by davorg on Nov 4, 2004, 2:22 AM)
|