A small point, but perhaps worth mentioning... You've got 3 grep's and a map. Each one of these things essentially does a foreach on its input. With Perl grep you can put anything that you want to into the grep - whatever that is, it only has to return a true or a false value. If true the input element from the right passes to the left. So all 3 grep's could be combined into one as shown below. Does the same thing but will run a bit faster - although usually of no importance.

It is also possible to combine all this into a single map although I don't think that I'd do it in this case because it reads to me as less clear...this is just an example of "how" should the desire arise... The important part here is the return value. Returning () from a map is different than returning undef. Undef is a value. () means "nothing is returned".

I guess as a side point, I like lining up logical conditions because human beings are "programmed" by instinct to process information better from top to bottom better than left to right and certainly better than diagonally. So when things don't fit nicely on one printed line, I do it like below.

my @files2 = grep { $_ ne "file1" and $_ ne "file2" and -f "$dir2/$_" }map { lc }readdir(DIR2); my @files = map { lc; ( $_ ne "file1" and $_ ne "file2" and -f "$dir2/$_" ) ? $_ :() }readdir(DIR2);

In reply to Re^3: Grep Array Multiple times by Marshall
in thread Grep Array Multiple times by PerlScholar

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.