I hope I understood your question

# This is untested code! my @dlcopy = @dir_list ; my @extensions = qw(zip log whatever) ; my %matches ; foreach my $ext (@extensions) { my @nonmathing ; my @matching ; foreach my $file (@dlcopy) { if ($file =~ /\.$ext$/) { push @matching,$file ; } else { push @nonmatching,$file ; } } @dlcopy = @nonmatching ; $matches{$ext} = \@matching ; }

That is: you work on a copy of @dir_list, iterating on it; you save in @nonmatching just the files that didn't match the pattern, and in @matching those that match. At the end of every cycle you shorten the inner foreach, since you are working only on those files that didn't match before. At the end of the game %matches contains all @dir_list elements that matched, and @dl_copy all those that didn't

Of course, we can heavily optimize this; the double foreach could be optimized, for example using ?:, array references and only one push instead of the if block. And, of course, the pattern matching could be "half-hardcoded" with an eval at each cycle...

But, at least, this should be clear code, even if untested :-)

Ciao!
--bronto

# Another Perl edition of a song:
# The End, by The Beatles
END {
  $you->take($love) eq $you->make($love) ;
}


In reply to Re: Best way to match items in an array by bronto
in thread Best way to match items in an array by ibanix

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.