Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: can I change hash keys or values directly

by choroba (Archbishop)
on Jan 28, 2021 at 21:34 UTC ( #11127607=note: print w/replies, xml ) Need Help??

in reply to can I change hash keys or values directly

There's no array of keys or values. There's a list of keys and list of values, and the list of values in fact contains the values themselves, so you can modify them, e.g.
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my %hash = (key1 => 'val1', key2 => 'val2'); s/val/V/ for values %hash; say "$_ => $hash{$_}" for keys %hash;

You can't modify the keys in this way, though (and both the facts are documented in keys and values. That's because you can't change a key in a hash, you need to remove the old one and create a new one, because the after a key change, the value is most probably going to be stored in a different place.

Using a slice is probably the best you can do.

my @old = keys %hash; @hash{map uc, @old} = delete @hash{@old};
Be sure to keep the keys unique after the modification!

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2021-04-14 08:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found