Hello misterperl,

Why not simply rename the key on the flow?

Sample of code:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test2', ); print Dumper \%hash; $hash{'key4'} = delete $hash{'key1'}; print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'key1' => 'test1', 'key3' => 'test2', 'key2' => 'test2' }; $VAR1 = { 'key2' => 'test2', 'key3' => 'test2', 'key4' => 'test1' };

Update: For multiple keys you can use hash slice:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test2', 'key4' => 'test4', ); print Dumper \%hash; my @oldKeys = sort keys %hash; print Dumper \@oldKeys; # Remove what ever keys you want to keep e.g. key1 key3 splice @oldKeys, 0, 1; splice @oldKeys, 1, 1; print Dumper \@oldKeys; my @newKeys = ('key5', 'key6'); @hash{@newKeys} = delete @hash{@oldKeys}; print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'key2' => 'test2', 'key3' => 'test2', 'key1' => 'test1', 'key4' => 'test4' }; $VAR1 = [ 'key1', 'key2', 'key3', 'key4' ]; $VAR1 = [ 'key2', 'key4' ]; $VAR1 = { 'key6' => 'test4', 'key5' => 'test2', 'key1' => 'test1', 'key3' => 'test2' };

Update2: Update the keys with for loop:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test3', ); print Dumper \%hash; my @new_keys = keys %hash; y/key[0-9]/alt[0-9]/ for @new_keys; @hash{@new_keys} = delete @hash{keys %hash}; print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test3' }; $VAR1 = { 'alt3' => 'test3', 'alt2' => 'test2', 'alt1' => 'test1' };

Update3: Also with map (I think this is slower):

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( 'key1' => 'test1', 'key2' => 'test2', 'key3' => 'test3', ); print Dumper \%hash; %hash = map { y/key[0-9]/alt[0-9]/r => $hash{$_} } keys(%hash); print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'key2' => 'test2', 'key1' => 'test1', 'key3' => 'test3' }; $VAR1 = { 'alt3' => 'test3', 'alt2' => 'test2', 'alt1' => 'test1' };

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: I want to operate on some values in a hash by thanos1983
in thread I want to operate on some values in a hash by misterperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.