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

Home: Perl Programming Help: Regular Expressions:
convert Farenheit to Celcius

yatinhat
Novice

Nov 1, 2004, 2:16 PM

Post #1 of 12 (24473 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 (24470 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 (24463 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 (24461 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 (24458 views)
 Re: [KevinR] 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.
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 (24450 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 (24448 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 (24414 views)
 Re: [KevinR] 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.
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 (24401 views)
 Re: [ShipOfFools] convert Farenheit to Celcius [In reply to] Can't Post
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 (24398 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 (24396 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 (24393 views)
 Re: [davorg] convert Farenheit to Celcius [In reply to] Can't Post
But you need the "no warnings 'numeric';" for that to work without generating warnings.

Understood. Thanks.

Dave

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0