Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Hash/Array slice : how to exclude items?

by johngg (Canon)
on Jan 24, 2023 at 13:58 UTC ( [id://11149819]=note: print w/replies, xml ) Need Help??


in reply to Hash/Array slice : how to exclude items?

Somewhat messy but you could grep a negated regex in a hash slice.

johngg@aleatico:~$ perl -Mstrict -Mwarnings -MData::Dumper -E 'say q{} +; my %hash = ( qw{ a 1 b 2 c 3 d 4 e 5 f 6 } ); print Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ *hash } ] ); my @excl = qw{ b f }; my $rxExcl = do { local $" = q{|}; qr{^@excl$}; }; print Data::Dumper->Dumpxs( [ [ @hash{ @{ [ grep { ! m{$rxExcl} } keys %hash ] } } ] ], [ qw{ *slice } ] );' %hash = ( 'd' => '4', 'b' => '2', 'a' => '1', 'c' => '3', 'e' => '5', 'f' => '6' ); @slice = ( '4', '1', '3', '5' );

I hope this is of interest.

Update: I renamed the second Data::Dumper->Dumpxs() output from @hash to @slice to better reflect what was going on.

Cheers,

JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149819]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2025-11-10 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (66 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.