Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Please Help

by cpom1 (Initiate)
on Sep 29, 2012 at 21:53 UTC ( [id://996426]=perlquestion: print w/replies, xml ) Need Help??

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

I am new to Perl scripting and I am writing a perl script for class: Write a script to help users calculate their tax returns. Prompt the user to enter his/her income from keyboard. Then use the following rules for calculation. Display the result on the screen. If the income is less than 5000, no tax; If the income is between 5000 and 30000, the tax rate is 10%; If the income is more than 30000, the tax rate is 20%. This is what I have so far:
#!/usr/bin/perl read -p "What is your name: " name print "Hi $name "; read -p "$name, please enter your income tax:" var1 if ($var1 -lt 5000) { print "no tax"; } elsif ($var1 -gt 5000 && $var1 -lt 30000) { print "the tax rate is 10%"; } elsif ($var1 -gt 30000) { print "the tax rate is 20%"; } print "\n";

Replies are listed 'Best First'.
Re: Please Help
by choroba (Cardinal) on Sep 29, 2012 at 22:06 UTC
    Hi cpom1, welcome to the Monastery.

    -lt is used in the shell. Perl uses common operators for numeric comparison $var 1 < 5000 (for strings, it uses $var gt "x", without the leading -).
    See perlop for details.

    BTW, your node title was not chosen luckily. See How do I compose an effective node title?.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Please Help
by kcott (Archbishop) on Sep 29, 2012 at 23:28 UTC

    G'day cpom1,

    Welcome to the monastery.

    The following should be sufficient information for you to complete your class assignment.

    The perldoc.perl.org site should provide most, if not all, of the documentation you need while initially learning Perl. I have it bookmarked and use it often; I suggest you do the same. I've included a number of links to documentation below - all to pages on this site.

    After the shebang line (that's the #!... on line 1) put:

    use strict; use warnings;

    When you do this, Perl will tell you about mistakes you've made or potentially problematic code you're attempting to run. While you're learning Perl, you should do this in all of your scripts. See strict and warnings.

    The line read -p "What is your name: " name is not Perl: it works in bash (and probably other shells). The typical way to achieve what you want here is with something like:

    print 'Prompt message: '; my $reply = <>; print $reply;

    The first line should be self-explanatory - single-quotes are for literal text (see print).

    In the next line my declares a lexical variable: Perl has various types of variables - you'll use this type most often and it's all you need for this type of script. Unlike bash, where you assign to varname then read from $varname, in Perl you use $varname in both cases. The <> reads from standard input (in your case, this is what you type at the keyboard). One important point to note is that it reads the text you type as well as the newline you enter to terminate your input. You can read about it in perlop - I/O Operators but you may find that rather heavy going: it certainly has far more information than you need for this script.

    Finally, you print whatever the user typed, including the newline.

    That handles getting the input you need. Note that there's no checking of this input: the user could have just typed a newline for name and something that isn't a number for income. I suspect that's not a requirement for your assignment: add a comment in your code stating that no validation is performed to indicate that you are aware of this limitation in your script. (If it is a requirement, come back and ask a follow-up question.)

    All of your conditions use bash (or other shell) code again (-lt and -gt). See perlop - Relational Operators for the Perl equivalents - note that there's different operators for string and numerical comparisons.

    That should be enough to get your script up and running. Here's a few additional links you may find useful: perl; perlsyn; perlstyle.

    -- Ken

Re: Please Help
by Kenosis (Priest) on Sep 30, 2012 at 00:02 UTC

    And another welcome to you! Do you have a question about your Perl script?

Re: Please Help
by grizzley (Chaplain) on Oct 01, 2012 at 09:30 UTC
    Not Perl-related answer, but shouldn't your task be to calculate tax? I mean if you have initial value x and you know what tax t is, you should print x * t on the output. For this you need to know that print takes list of arguments and print all of these, so you can use something like this: print "the tax rate is", 20, "% and it is ", 20 * $var1
Re^2: Please Help
by Generoso (Prior) on Sep 30, 2012 at 02:53 UTC

    After all the explaining above here is how you script should look like.

    #!/usr/bin/perl use strict; use warnings; $| =1; print 'What is your name: '; my $reply = <>; chomp($reply); print $reply; print "\nHi $reply \n"; print "$reply, please enter your income tax:" ; my $var = <>; if ($var < 5000) { print "no tax"; } elsif ($var > 5000 && $var < 30000) { print "the tax rate is 10%"; } elsif ($var > 30000) { print "the tax rate is 20%"; } print "\n";
      Thank all of you for your help. You guys have helped a great deal. And I have definitely bookmarked this site along with perldoc.perl.org :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found