CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: [ShipOfFools] convert Farenheit to Celcius: Edit Log



davorg
Thaumaturge / Moderator

Nov 4, 2004, 2:20 AM


Views: 9574
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:


Code
#!/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)


Edit Log:
Post edited by davorg (Thaumaturge) on Nov 4, 2004, 2:22 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives