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

yatinhat
Novice

Nov 1, 2004, 2:16 PM

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

Views: 27197
 Re: [yatinhat] convert Farenheit to Celcius
 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

Views: 27190
 Re: [yatinhat] convert Farenheit to Celcius
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

Views: 27188
 Re: [ShipOfFools] convert Farenheit to Celcius
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

Views: 27185
 Re: [KevinR] convert Farenheit to Celcius
 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

Views: 27177
 Re: [ShipOfFools] convert Farenheit to Celcius
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

Views: 27175
 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)

ShipOfFools
Novice

Nov 4, 2004, 12:55 PM

Views: 27141
 Re: [KevinR] convert Farenheit to Celcius
 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

Views: 27128
 Re: [ShipOfFools] convert Farenheit to Celcius
 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

Views: 27125
 Re: [davorg] convert Farenheit to Celcius
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

Views: 27123
 Re: [ShipOfFools] convert Farenheit to Celcius
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

Views: 27120
 Re: [davorg] convert Farenheit to Celcius
 In Reply To
But you need the "no warnings 'numeric';" for that to work without generating warnings.

Understood. Thanks.

Dave