Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
We don't bite newbies here... much
 
PerlMonks  

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

( #376389=categorized question: print w/ replies, xml ) Need Help??
Contributed by NetWallah on Jul 21, 2004 at 21:38 UTC
Q&A  > hashes


Description:

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

Answer: How do I modify the KEYS in a hash (copy)
contributed by NetWallah

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.
Answer: How do I modify the KEYS in a hash (copy)
contributed by jdporter

my %Copy; @Copy{ map { tr/ /_/; $_ } keys %Orig } = values %Orig;
Answer: How do I modify the KEYS in a hash (copy)
contributed by NetWallah

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"

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (5)
    As of 2014-04-18 04:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (461 votes), past polls