snowsurfer has asked for the wisdom of the Perl Monks concerning the following question:

I am new to programming and I have chosen PERL to learn. I have the book "Teach yourself PERL in 21 days" I am on day 3, and I have decided to create some small scripts to practice. I created a script that asks for your hourly pay rate and calculates your yearly salary. I also made one in reverse. The problem I am having is combining them. I am trying to get it to ask "Do you wish to calculate yearly salary (y) or hourly rate?(h)" If you enter h it performs the hourly to yearly calculation, if you choose y it performs the yearly to hourly. If you dont choose h or y it asks you again. I am stuck in a proverbial loop. Here is my code
#!/usr/local/bin/perl -w $input = ''; #temporary input $hrate = 0; #pay rate $yrate = 0; #yearly salary while () { print 'Do you wish to calculate yearly salary (y) or hourly rate?( +h)'; chomp ($input = <STDIN>); if ($input eq '') { last; } if ($input =~ /h/) { print "Enter your hourly rate:"; chomp ($hrate = <STDIN>); $yrate = ($hrate * 40) *52; } }print "Your yearly salary is $yrate";

Replies are listed 'Best First'.
Re: New to Programming and I have chose PERL
by Tanktalus (Canon) on Jan 12, 2005 at 03:46 UTC

    Move your print statement to inside the braces for the if statement.

    I'd also suggest looking at reformating a bit so you can more easily see what goes together:

    #!/usr/local/bin/perl -w $input = ''; #temporary input $hrate = 0; #pay rate $yrate = 0; #yearly salary while () { print 'Do you wish to calculate yearly salary (y) or hourly rate?( +h)'; chomp ($input = <STDIN>); if ($input eq '') { last; } if ($input =~ /h/) { print "Enter your hourly rate:"; chomp ($hrate = <STDIN>); $yrate = ($hrate * 40) *52; print "Your yearly salary is $yrate\n"; } }
    (I'm not going to get into the One True Brace Style religious flamefest - so we'll just use the brace positioning you're already using.)

    Hope that helps.

      I'm sure this is overkill ( I saw others using regexs so I thought I'd go all out. ;)), but this is how I like to do command line driven programs like this. Basically I'm creating a lookup table with the %actions hash. When someone enters their choice, it checks to see if that choice exists in the lookup table, if it does, it'll execute the sub reference that the lookup table points to. If the choice does not exist in the lookup table, it goes on to throw the error and starts the loop over again. While I understand you're new to perl, this may give you a little something to chew on while you learn. :-D
      use strict; my %actions = ( 'y' => \&calcHourly, 'h' => \&calcYearly, 'q' => sub { exit(); }, ); while ( 1 ) { print "\nEnter y (or) h (or) q to calculate Yearly salary(y) or Ho +urly rate(h) q to Quit: "; chomp( my $myAction = <STDIN> ); if ( exists $actions{ $myAction } ) { $actions{ $myAction }->(); next; } print "\nError: \"$myAction\" is not a valid choice, please try ag +ain\n\n"; } sub calcHourly { print "Enter your hourly rate: "; chomp ( my $hourrate = <STDIN> ); my $yearrate = ( $hourrate * 40 ) * 52; print("\nYour Annual salary is: \$$yearrate\n\n"); } sub calcYearly { print "Enter your yearly salary: "; chomp ( my $yearrate = <STDIN> ); my $hourrate = ( $yearrate / 52 ) / 40; print("\nYour hourly rate is: \$$hourrate\n\n"); }
      (oops, replied to the wrong one, doh!)
Re: New to Programming and I have chose PERL
by TStanley (Canon) on Jan 12, 2005 at 04:00 UTC
    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
Re: New to Programming and I have chose PERL
by kamesh3183 (Beadle) on Jan 12, 2005 at 06:25 UTC
    I would have written the above logic like this
    #!/usr/local/bin/perl use strict; use warnings; my $input; my $hrate = 0; my $yrate = 0; while (!defined $input) { print "\nDo you wish to calculate yearly salary (y) or hourly rate + (h)?"; chomp ($input = <STDIN>); if ($input =~ /h/i) { print "\nEnter your yearly salary:"; chomp ($yrate = <STDIN>); $hrate = ($yrate / 52) / 40; print "\nYour hourly rate is $hrate"; }elsif ($input =~ /y/i) { print "\nEnter your hourly rate:"; chomp ($hrate = <STDIN>); $yrate = ($hrate * 40) * 52; print "\nYour yearly salary is $yrate"; }elsif ($input =~ /q/i) { exit; }else { print "\nError in input"; } undef $input; #to continue in the loop }
    I would like to suggest that a good place to start learning Perl is to read "Learning Perl", published by O'Reilly & Associates. And one more book is freely available online. Name is Simon Cozens' Beginning Perl.
Re: New to Programming and I have chose PERL
by perlsen (Chaplain) on Jan 12, 2005 at 10:22 UTC

    Hai just try this if u wish

    my($read,$hourrate,$yearrate); while(!defined) { print "\nEnter y (or) h (or) q to calculate Yearly salary(y)\n or Ho +urly rate(h) q to Quit: "; chomp($read=<STDIN>); if($read=~m#y#i) { print "\nEnter your yearly salary: "; chomp ($yearrate=<STDIN>); $hourrate=($yearrate/52)/40; print("\nYour salary is for per hour: $hourrate\n\n"); } elsif($read=~m#h#i) { print "\nEnter your hourly rate: "; chomp ($hourrate=<STDIN>); $yearrate=($hourrate * 40)*52; print("\nYour Annual salary is for per hour: $yearrate\n\n"); } elsif($read=~m#q#i) { exit; } }
Re: New to Programming and I have chose PERL
by pearlie (Sexton) on Jan 12, 2005 at 10:41 UTC
    Hello, I think in the first if loop, you need to use "next;" instead of "last;"

    Also, in the if loop matching 'h', you need to put a last statement in the end to exit out of while loop.

    Another if loop can be added to match 'y'.

    Also, you can combine all the if loops using elsif.

Re: New to Programming and I have chose Perl
by kelan (Deacon) on Jan 12, 2005 at 14:15 UTC

    As you are new to Perl, I would suggest that you use the correct capitalization of it. Folklore has lead to the belief that it is an acronym, but looking at the last sentence of this FAQ, you can see that that isn't really the case. If that still doesn't convince you, think of it as a personal favor to me:) Thanks.

Re: New to Programming and I have chose Perl
by Random_Walk (Prior) on Jan 12, 2005 at 17:31 UTC

    In the spirit of TIMTOWTDI here is an atempt to get it into a one liner, for some definitions of one line anyway :)

    perl -e'while(1){print"enter h/y:";$_=<>;next unless/^[hy]$/;($s,$m)=/ +h/?(yearly,2080):(hourly,1/2080);print"enter rate:";$_=<>;print"your +$s rate is ",$_*$m,$/}' # or expanding the same idea into a proper program #!/usr/bin/perl use strict; use warnings; my $hours=40*52; # are you mad ? while(1){ print 'Do you wish to calculate yearly salary (y) or hourly rate?( +h): '; chomp($_=<>); last unless $_; next unless/^[hy]$/; my @words=("yearly salary", "hourly rate"); my ($ask, $say,$multiplier)=/y/?(1, 0, $hours):(0, 1, 1/$hours); print "Enter your $words[$ask]: "; $_=<>; print"your $words[$say] is ", $_*$multiplier, "\n\n" }

    Cheers,
    R.

Re: New to Programming and I have chose PERL
by jkeenan1 (Deacon) on Jan 12, 2005 at 21:37 UTC
    I have the book "Teach yourself PERL in 21 days" I am on day 3 ...

    A book I used when teaching myself Perl five years ago. And a book riddled with typographical errors. I probably learned more Perl correcting the typos there than I did from the book itself.

    When I got frustrated with that book, I would switch off and work through a few chapters in Randal L Schwartz's Learning Perl (O'Reilly), now in its third edition and popularly known as the Llama book. I subsequently taught introductory Perl from Randal's book. I've had many occasions to refresh my knowledge by looking something up in the Llama, but have only referred back to "21 Hours."

    Jim Keenan

Welcome, dude!
by davebaker (Pilgrim) on Jan 12, 2005 at 19:50 UTC
    Welcome, dude! I love Perl. I hope you'll contribute the questions and difficulties you'll encounter along your learning curve; it'll help us to make Perl better and more user-friendly. You've come to the right place!