A few comments that I believe are very important about your posted code.
  1. The sequence $100 appears in your double-quote delimited string on line 2. Because double-quote delimited strings interpolate (Quote and Quote like Operators), Perl sees this as a variable. The variable $100 is the hundredth capture buffer from a regular expression. Had you actually tested what you posted, perl would have thrown a warning (Use of uninitialized value in concatenation (.) or string). You should not be posting code that throws warnings unless the warning pertains to your question. See How (Not) To Ask A Question.

  2. As we already discussed in counting number of occurrences of words in a file, why are you not using strict? If you opt not to use strict, why are you using my?

  3. You should never use $a as an ordinary variable. It has special meaning for sort. The only one-character variables which are socially acceptable to use as generic variables are $i, $j, $x, $y and $z, because these have established meanings that predate Perl (and microprocessors). Any time you use a variable with a single character for its name, you run a very real risk of clobbering some internal Perl behavior.

  4. Is your dividend 3.78% or 6.78%? Your code seems confused on that issue.

  5. Why do you create a hash and then sort the keys on your hash for output when you already have those keys in order in @stocknum? Why not just iterate over an index, or at least use the existing array as your list?

  6. Why do you concatenate two strings for your output? You are adding unnecessary operations, confusion and characters.

  7. Why do you have a floating $dividend variable at the end of your script, who's only function is to throw another pointless warning?

  8. Your indentation still leaves something to be desired. Check out perltidy as a way to make your code prettier.

This code should probably not be written as a recursive from an efficiency standpoint - a for or while loop is pretty good, while of course the best way to do it is CountZero's exponentiation. However, that was your question and understanding how to build a recursive subroutine is good. For detailed explanations, I like the Recursion And Callbacks chapter from Higher-Order Perl, which is freely available for download. Post an attempt at a recursive subroutine, and we'll help you debug it. Please note that writing recursive subroutines without strict nearly always results in bugs. Good luck.

In reply to Re: dividend with recursive routine by kennethk
in thread dividend with recursive routine by derpp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.