> Shouldn't this work?:

> print Dumper(do { delete local  @$hashref{ @$bad_keys }, $hashref} ); or this print Dumper(do { delete local  @$hashref{ @$bad_keys }; $hashref} );

yes it's surprising.

One needs to remember that local is protecting certain entries and not the whole hash (The hash could be a lexical variable, local wouldn't do anyway)

Since you are returning a reference, you'll see the restored entries again after leaving the block.

consider what's happening without delete

DB<91> use Data::Dump qw/pp/ DB<92> ; {local @hash{qw/log schema/};say pp \%hash}; say pp \%hash { a => 1, b => 2, c => 3, log => undef, schema => undef } { a => 1, b => 2, c => 3, log => "l", schema => "s" }

you can still have the desired effect by just returning a list or a new anonymous hash

DB<96> say pp do {delete local @hash{qw/log schema/}; %hash }; say +pp \%hash ("a", 1, "c", 3, "b", 2) { a => 1, b => 2, c => 3, log => "l", schema => "s" } DB<97> say pp do {delete local @hash{qw/log schema/}; +{%hash} }; s +ay pp \%hash { a => 1, b => 2, c => 3 } { a => 1, b => 2, c => 3, log => "l", schema => "s" } DB<98>

if you shy away from the overhead to return a new hash, just do the dumping inside the block.

DB<99> ; {delete local @hash{qw/log schema/};say pp \%hash}; say pp +\%hash { a => 1, b => 2, c => 3 } { a => 1, b => 2, c => 3, log => "l", schema => "s" } DB<100>

HTH! :)

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery


In reply to Re^4: Hash/Array slice : how to exclude items? by LanX
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.