Monks,
Being new with Perl I am stumbling about in confusion. I am attempting to write a simple calculator which takes the elements from a user entered array (consisting of more than 2 elements), then adds, subtracts, etc.. the elements.
The problem I am having is how to keep a running result of the subtraction value. I only know how to subtract single elements such as:
$c = $a - $b;
What I am looking to do is get a final subtracted value. This is where I have stalled:
... code ...
sub subtraction{
chomp(@_ = split(" ", <STDIN>)); # user enters values on one line
foreach(@_) {
$result -= $_; # I guess I am using the WRONG operator
}
print "Result: $result\n\n";
... rest of subroutine code ...
}
So if the array was 4 3 1, the result would be (4 - 3 - 1) = 0
Your help has always been timely and invaluable in the past. Any hints as to the proper way to implement this code are very appreciated since I plan to extrapolate (if possible) your aid into the multiplication and division components of this script. Thank you.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.