While your algorithm works (mostly), it trades a small memory gain for a lot of extra CPU work, especially as @do_not_want grows.

For every key in %hash, the code must loop through every member of @do_not_want. Grep does not short-circuit, so even after you've found 'ytd', you are still going to check 'mtd' and 'wtd'.

The other problem with this code is that if $hash{ytd_total} = 1000; is a desired report value, your grep will match and incorrectly exclude that value.

As long as pure textual equivalence is the matching criteria, using a hash for the lookup is going to be faster and make a tiny impact on memory.

If you need to do some kind of complex matching, then there is no way (that I know of) to avoid nested loops. But you can use a short circuited loop to avoid checking for any matches after the first. List::MoreUtils any comes to mind as an excellent way to handle this situation.

Here's a version that uses a hash

my %hash = ( ytd => 1.5, mtd => 2.0, wtd => 2.5, Jumbo_Tron => "United Center", Meat_Pie => "Gross", "Word" => "Association", ); my %do_not_want; @do_not_want{ qw(ytd mtd wtd) } = (); foreach my $k (keys %hash) { next if exists $do_not_want{$k}; print "$k => $hash{$k}\n"; }

Here's a version that matches hash keys against a regex:

use List::MoreUtils qw(any); my %hash = ( ytd => 1.5, mtd => 2.0, wtd => 2.5, Jumbo_Tron => "United Center", Meat_Pie => "Gross", "Word" => "Association", ); my @do_not_want = ( qr/ytd/, qr/mtd/, qr/wtd/ ); foreach my $k (keys %hash) { next if any { $k =~ $_ } @do_not_want ; print "$k => $hash{$k}\n"; }

Warning, all this code is untested. I modified it in a browser edit box.


TGI says moo


In reply to Re^2: Selecting a Specific Keys in Relation to a List by TGI
in thread Selecting a Specific Keys in Relation to a List by awohld

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.