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

How do I modify the KEYS in a hash (copy)

by NetWallah (Canon)
on Jul 21, 2004 at 21:38 UTC ( [id://376389]=perlquestion: print w/replies, xml ) Need Help??

NetWallah has asked for the wisdom of the Perl Monks concerning the following question: (hashes)

How to replace the spaces with underscores, in the Keys of a hash? (Or perform other transformation on the KEYS ?)

Originally posted as a Categorized Question.

  • Comment on How do I modify the KEYS in a hash (copy)

Replies are listed 'Best First'.
Re: How do I modify the KEYS in a hash (copy)
by NetWallah (Canon) on Jul 21, 2004 at 21:45 UTC
    To modify keys of a hash, it is necessary to copy it. The keys can be modified in the process of the copy by using 'map' like this:
    my %Orig=('a b'=>44, 'c d'=>22, rr=>99); my %Copy = map {my $y=$Orig{$_}; #Save Copy of Val tr/ /_/; $_ => $y} keys %Orig; print qq(C: $_ => $Copy{$_}\n) for keys %Copy; print qq(O: $_ => $Orig{$_}\n) for keys %Orig;
    The trick here is to save a copy of the Value into "$y" before "$_" is modified.
    Once $_ is modified, the $Orig{$_} is no longer valid, so it must be saved into $y first.
Re: How do I modify the KEYS in a hash (copy)
by jdporter (Paladin) on Feb 06, 2008 at 15:15 UTC
    my %Copy; @Copy{ map { tr/ /_/; $_ } keys %Orig } = values %Orig;
Re: How do I modify the KEYS in a hash (copy)
by NetWallah (Canon) on Jul 29, 2004 at 17:09 UTC
    Here is another way to modify KEYS in-place.

    This would be efficient in the case where there were a relatively small number of keys that required modification.

    Use the previous method (copy the hash) if the number of keys requiring modification is large.

    my %o=(this=>33, 'th at'=>44, other=>55,'some thing'=>66,'or another'= +>77,none=>88); while (my ($k,$v)=each %o){ my $ModifiedK = $k; next unless $ModifiedK =~s/\s/_/; delete $o{$k}; #This is SAFE !! RTFM. $o{$ModifiedK}=$v; }; print qq(Mod: $_\t=>$o{$_} \n) for sort keys %o"
Re: How do I modify the KEYS in a hash (copy)
by sen (Hermit) on Feb 06, 2008 at 11:56 UTC

    Here is another way to modify the keys.

    my %Orig=('a b'=>44, 'c d'=>22, rr=>99); my %Copy = map { $_ => $_=~ s/ /_/ } keys %Orig; print qq(C: $_ => $Copy{$_}\n) for keys %Copy;

    Originally posted as a Categorized Answer.

Re: How do I modify the KEYS in a hash (copy)
by sen (Hermit) on Feb 06, 2008 at 14:42 UTC

    Here is another way to modify the keys.

    my %Orig=('a b'=>44, 'c d'=>22, rr=>99); my %Copy = map { $_ => $_=~ s/ /_/ } keys %Orig; print qq(C: $_ => $Copy{$_}\n) for keys %Copy;

    Originally posted as a Categorized Answer.

Re: How do I modify the KEYS in a hash (copy)
by sen (Hermit) on Feb 06, 2008 at 14:42 UTC

    Here is another way to modify the keys.

    my %Orig=('a b'=>44, 'c d'=>22, rr=>99); my %Copy = map { $_ => $_=~ s/ /_/ } keys %Orig; print qq(C: $_ => $Copy{$_}\n) for keys %Copy;

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-16 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found