I've read in the contents of a directory and I would like to sort them nicely: Subdirectories on top (in alphabetical order) and then files (in alphabetical order). I'm using the following code, which takes a reference to the array containing the directory contents as a parameter:
sub SortDirectory() { my $arrayRef = $_[0]; my @fileArray; my $currIndex = 0; my $i; for ( $i = 0; $i < @$arrayRef; $i++ ) { if ( (grep /\./, $$arrayRef[$i]) != "" ) { $fileArray[$currIndex++] = splice(@$arrayRef,$i--,1); } } push (@$arrayRef, @fileArray); }
This seems to work except for the directories "." and "..", which I would like to appear at the very top of the directory listing (hence, be placed at the beginning of the array). Since I'm just checking for the presence of a "." to determine if the entry is a subdirectory or a file, the directories "." and ".." are considered files as well.

I was wondering if there was a way to have grep search for a wildcard (i.e. look for a "." with any other character immediately after it). That might help me solve the problem. Of course, if someone knows a better way to accomplish what I'm doing, I'm open to suggestions at this point.

Thanks,
- Sherlock

In reply to Using grep with wildcards by Sherlock

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.