Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: can I change hash keys or values directly

by choroba (Cardinal)
on Jan 28, 2021 at 21:34 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-19 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found