Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: can I change hash keys or values directly

by tobyink (Canon)
on Jan 30, 2021 at 21:48 UTC ( #11127698=note: print w/replies, xml ) Need Help??


in reply to can I change hash keys or values directly

Can't do it with built-ins, but it's easy to create a function that can do it.

#!perl use strict; use warnings; BEGIN { package Hash::Adjust; use Exporter::Shiny 'ha'; sub _generate_ha { my ( $me, $name, undef, $opt ) = ( shift, @_ ); my ( $aref, $bref ) = do { no strict 'refs'; my $caller = $opt->{into}; ( \${"$caller\::a"}, \${"$caller\::b"} ); }; return sub (&\%) { my ( $code, $hashref ) = ( @_ ); @_ = (); my %tmp; while ( ( $$aref, $$bref ) = each %$hashref ) { &$code; $tmp{$$aref} = $$bref if defined $$aref; } %$hashref = %tmp; }; } }; use Hash::Adjust 'ha'; use Data::Dumper; my %hash = ( FOO => 42, BAR => 666 ); ha { $a =~ s/O/o/g; ++$b; } %hash; print Dumper \%hash;

The ha function is called like ha { CODE } %hash and modifies the keys and values of the hash. The code gets the key in $a and the value in $b; the same special global variables used by sort. If the code block sets $a = undef then that key-value pair will be removed from the hash.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2021-10-17 18:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (72 votes). Check out past polls.

    Notices?