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:
convert Farenheit to Celcius

 



yatinhat
Novice

Nov 1, 2004, 2:16 PM

Post #1 of 12 (10348 views)
convert Farenheit to Celcius Can't Post

I am trying to teach myself Perl.

I am trying to write a script to convert Farenheit to Celcius and vice versa. I want the program to accept any number followed by an F or a C in upper or lower case. There may be spaces between the number and the letter. Numbers may have a (+ or -) and may be integer or a float.

The program is not working yet...but I know how I want it to work.
Attachments: convert.pl (0.58 KB)


davorg
Thaumaturge / Moderator

Nov 2, 2004, 3:16 AM

Post #2 of 12 (10345 views)
Re: [yatinhat] convert Farenheit to Celcius [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;

print "Enter a temperature (e.g. 32F 100C):\n";
my $input = <STDIN>;
chomp ($input);
if ($input =~ m/^([-+]?\d+\.?\d*)\s*([CF])$/i) {
my $InputNum = $1;
my $type = $2;

my ($C, $F);

if ($type eq "C" or $type eq "c") {
$C = $InputNum;
$F = ($C * 9/5) + 32;
} else {
$F = $InputNum;
$C = ($F - 32) * 5/9;
}
printf"%.2f C = %.2f F\n", $C, $F;
} else {
print "Expecting a temperature, so I don't understand '$input'.\n";
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ShipOfFools
Novice

Nov 3, 2004, 5:31 PM

Post #3 of 12 (10338 views)
Re: [yatinhat] convert Farenheit to Celcius [In reply to] Can't Post

I heard that regex's are inefficient; that they should be used conservatively.
Anyway, here's how to convert Fahrenheit (note the spelling because that's someone's name) into Celcius...


Code
   

print C( $ARGV[0] );

sub C
{
return (5/9)*($_[0] - 32);
}



...where $_[0] = F.

Perform a simple algebraic manipulation to find F.

Dave


KevinR
Veteran


Nov 3, 2004, 6:01 PM

Post #4 of 12 (10336 views)
Re: [ShipOfFools] convert Farenheit to Celcius [In reply to] Can't Post

all this regexp is doing is checking for a valid entry:

if ($input =~ m/^([-+]?\d+\.?\d*)\s*([CF])$/i) {

its not performing the conversion.
-------------------------------------------------


ShipOfFools
Novice

Nov 3, 2004, 7:19 PM

Post #5 of 12 (10333 views)
Re: [KevinR] convert Farenheit to Celcius [In reply to] Can't Post


In Reply To
all this regexp is doing is checking for a valid entry:

if ($input =~ m/^([-+]?\d+\.?\d*)\s*([CF])$/i) {

its not performing the conversion.

Yes I know. But if you follow all the steps through that code, you must agree that there is a more efficient way. All you have to do is check for C or F and have your answer ready. In fact you can use part of the actual input as a function name.

Dave


KevinR
Veteran


Nov 3, 2004, 10:55 PM

Post #6 of 12 (10325 views)
Re: [ShipOfFools] convert Farenheit to Celcius [In reply to] Can't Post

I know davorg can speak for himself and I am sure he would prefer to speak for himself if he wants to. I'm just a hack coder with Perl myself and would not feel at all comfortable discussing the merits of any code that davorg posts.

Its been my experience that he is a generous person with his time and abilities and critiquing his code is something I am eminently unqualified to do.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Nov 4, 2004, 2:20 AM

Post #7 of 12 (10323 views)
Re: [ShipOfFools] convert Farenheit to Celcius [In reply to] Can't Post

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)


ShipOfFools
Novice

Nov 4, 2004, 12:55 PM

Post #8 of 12 (10289 views)
Re: [KevinR] convert Farenheit to Celcius [In reply to] Can't Post


In Reply To
I know davorg can speak for himself and I am sure he would prefer to speak for himself if he wants to. I'm just a hack coder with Perl myself and would not feel at all comfortable discussing the merits of any code that davorg posts.

Its been my experience that he is a generous person with his time and abilities and critiquing his code is something I am eminently unqualified to do.

Hey I wasn't bagging davorg. My post was aimed at yatinhat. I just thought I'de mention that I heard regex's should be used conservatively.

I'm going to be needing some help in the future with Perl. This will be the first place I come to because I can see that davorg is an excellent Perl programmer and very helpful. And of course, there's also your good self! :)

Just one little thing though...


Code
   

$subs{uc $1}->($ARGV[0])

Shouldn't that call pass $ARGV[1]?

Dave


davorg
Thaumaturge / Moderator

Nov 5, 2004, 1:20 AM

Post #9 of 12 (10276 views)
Re: [ShipOfFools] convert Farenheit to Celcius [In reply to] Can't Post


In Reply To
Just one little thing though...


Code
$subs{uc $1}->($ARGV[0])

Shouldn't that call pass $ARGV[1]?


No. It's fine the way it is. @ARGV only contains one value (well, I suppose it can contain as many values as the user wants, but we only process the first one).

Perhaps I'm missing something. Why do you think it should be $ARGV[1]?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ShipOfFools
Novice

Nov 5, 2004, 2:09 AM

Post #10 of 12 (10273 views)
Re: [davorg] convert Farenheit to Celcius [In reply to] Can't Post

Sorry. I didn't try "\d+[cf]". I tried "[cf]\d+" (doesn't work), and "[cf] \d+". The latter works when $ARGV[1] is passed. I didn't know you could do things like ("100followed by chars" - 32)...nice little tip.

Dave.


davorg
Thaumaturge / Moderator

Nov 5, 2004, 2:18 AM

Post #11 of 12 (10271 views)
Re: [ShipOfFools] convert Farenheit to Celcius [In reply to] Can't Post

But you need the "no warnings 'numeric';" for that to work without generating warnings.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ShipOfFools
Novice

Nov 5, 2004, 6:51 AM

Post #12 of 12 (10268 views)
Re: [davorg] convert Farenheit to Celcius [In reply to] Can't Post


In Reply To
But you need the "no warnings 'numeric';" for that to work without generating warnings.



Understood. Thanks.

Dave

 
 


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

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