x +{ %hash{grep !/^schema|log$/, keys %hash } }

$exclude = sub  { my $href = shift;  my $excl= join '|', @_; return  +{ %$href{ grep !/^$excl$/, keys %$href } } }

Noting, of course, that because there are no parentheses in either of these regex, the ^ anchor only applies to the first term in the alternation list, the $ anchor only applies to the final term in the alternation list, and any intermediate terms will be completely unanchored, which means that it's likely to exclude more keys than you might think: therefore, running ->exclude(qw/log schema/) will not necessarily match running ->exclude(qw/schema log/) , depending on the keys of the hash, which may or may not have been the intention.

DB<1> @hash{'schema','log',a..c, 'schematic', 'blog'}=('s','l',1..3, +'circuit','rant') DB<2> x \%hash 0 HASH(0x3da37a8) 'a' => 1 'b' => 2 'blog' => 'rant' 'c' => 3 'log' => 'l' 'schema' => 's' 'schematic' => 'circuit' DB<3> x +{ %hash{grep !/^schema|log$/, keys %hash } } # Note that t +he ^ only applies to schema, and the $ only applies to log, so it wil +l remove more than you might have meant 0 HASH(0x3f6e6f8) 'a' => 1 'b' => 2 'c' => 3 DB<4> x +{ %hash{grep !/^(schema|log)$/, keys %hash } } # This vers +ion has all exclusions fully anchored 0 HASH(0x3f6ed10) 'a' => 1 'b' => 2 'blog' => 'rant' 'c' => 3 'schematic' => 'circuit' DB<5> $exclude1 = sub { my $href = shift; my $excl= join '|', @_; +return +{ %$href{ grep !/^$excl$/, keys %$href } } } DB<6> $h_ref = \%hash DB<7> x $h_ref->$exclude1(qw/schema log/) 0 HASH(0x3f73640) 'a' => 1 'b' => 2 'c' => 3 DB<8> x $h_ref->$exclude1(qw/log schema/) 0 HASH(0x3f73988) 'a' => 1 'b' => 2 'blog' => 'rant' 'c' => 3 'schematic' => 'circuit' DB<9> $exclude2 = sub { my $href = shift; my $excl = join '|', @_; r +eturn +{ %$href{ grep !/^($excl)$/, keys %$href } } } DB<10> x $h_ref->$exclude2(qw/schema log/) 0 HASH(0x3f73808) 'a' => 1 'b' => 2 'blog' => 'rant' 'c' => 3 'schematic' => 'circuit'

In reply to Re^3: Hash/Array slice : how to exclude items? -- be careful of anchoring by pryrt
in thread Hash/Array slice : how to exclude items? by bliako

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.