Nice idea, I can see how this would come in handy. However, there are a few ways in which this code could be cleaned up a bit.

First, you are using local in places where my would be better. See The difference between my and local for a more indepth discussion... I'd recommend replacing every instance of local with my in the code above.

Second, you have the comment:

#NOTE THE CODE: local($nmbr_items) = @some_array; #DOESNT WORK SO ALWAYS DECLARE IT FIRST.
Which is a *great* comment, because it lets me know what you're thinking and helps me understand why the code is written the way it is. Lets assume you've already followed my advice above and replaced local($nmbr_items) with my($nmbr_items) You'll still have the same issue mentioned in the comment, namely that wrapping  my( ... ) around a variable can alter things somewhat subtly. When used in this manner, my will impose a list context onto the right hand side of the assignment. The return value resulting from evaluating an array in scalar vs. list context is different, so you get different results. The solution is to drop the parens and modify the code to read my $nmbr_items = ... This will behave how you want it to.

Finally, the structure of the code is:

while (read directory) { add to temp array; } examine array to set up loop vars for next loop while (loopvars tell us were still in the loop) { generate return array }
Why not combine the directory reading loop and the return value generating loop.. This elimiates will greatly simplify the code, down to something such as:
my @returnvalue; while(read directory) { skip some bad entries ('.' , '..', and non-dirs) add value to @returnvalue } return @returnvalue;

-Blake


In reply to Re: Return an Array of Sub-Dir Names by blakem
in thread Return an Array of Sub-Dir Names by cynix

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.