Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: New to Programming and I have chose PERL

by TStanley (Canon)
on Jan 12, 2005 at 04:00 UTC ( [id://421492]=note: print w/replies, xml ) Need Help??


in reply to New to Programming and I have chose PERL

Welcome to PerlMonks!
Here is how I would have done your code:
#!/usr/bin/perl -w use strict; my($input,$hrate,$yrate,$x); while(!defined $x){ print "Do you wish to calculate yearly salary (y) or hourly rate (h) +:"; chomp($input=<STDIN>); if($input=~/y/i){ # I am just guessing that this is what you're # trying to do print "Enter your yearly salary: "; chomp ($yrate=<STDIN>); $hrate=($yrate/52)/40; printf("You make \$%2d per hour.",$hrate); $x=1; }elsif($input=~/h/i){ print "Enter your hourly rate: "; chomp ($hrate=<STDIN>); $yrate=($hrate * 40)*52; print"\nYour yearly salary is \$$yrate\n"; $x=1; }else{ print"Please enter either h or y.\n\n"; sleep 2; ) }
By using strict, you will catch accidental misspellings and make you declare your variables. The way that your code is right now, you don't see the last line of it,until you do a control-C out of the program. Mine will do one of the calculations then exit. When checking the input with a regular expression, I used the "i" modifier which means that if the user has their caps lock key on, it will still accept it.

TStanley
--------
The only thing necessary for the triumph of evil is for good men to do nothing -- Edmund Burke

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://421492]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found