Hello bliako,

very interesting question and discussion. I am not able to offer a solution but this lead to me to something different: temporary hide or mask the value of some key of a tied hash.

Mostly copying ikegami's SO answer I got something working.

I'm sure some monk can play also with FIRTSKEY and NEXTKEY vodoo to make some key temporary invisible (see the comment line in fetch..), not me :)

package MaskKeys; use strict; use warnings; use Tie::Hash (); our @ISA = 'Tie::ExtraHash'; sub TIEHASH { my $class = shift; my $content = shift; my $tomask = shift; my $self = bless([{@$content}, {}]); $self->[1]{$_} = 1 for @$tomask; return $self; } sub FETCH { my $self = shift; my $key = shift; if ( $self->[1]{$key} ){ return 'NA'; # {return $self->{ $self->FETCH($self->SUPER::NEXTKEY ($ke +y))} || undef ; } else { return $self->[0]{$key} } } sub mask_key{ my $self = shift; my $key = shift; $self->[1]{$key} = 1; } sub unmask_key{ my $self = shift; my $key = shift; # delete @{ $self->[1] }{ $key }; unecessary from cut and paste co +de that accepted a list delete $self->[1]{$key}; } package main; # content # to mask tie my %hash, 'MaskKeys',[schema=>11111, aaaaaa=>33333], ['schema']; print "\noriginally masked: \n"; print "$_ -> $hash{$_}\n" for keys %hash; print "\nunmasked: \n"; tied(%hash)->unmask_key('schema'); print "$_ -> $hash{$_}\n" for keys %hash; print "\nmasked again: \n"; tied(%hash)->mask_key('schema'); print "$_ -> $hash{$_}\n" for keys %hash; __END__ originally masked: aaaaaa -> 33333 schema -> NA unmasked: aaaaaa -> 33333 schema -> 11111 masked again: aaaaaa -> 33333 schema -> NA

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Hash/Array slice : how to exclude items? -- tie hash to mask values by Discipulus
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.