How to replace a Perl hash key?

后端 未结 3 858
无人及你
无人及你 2020-12-10 11:10

Let us say if I have a hash like this:

$data = {
    \'key1\' => {
                \'key2\' => \'value1\'
              },
    \'key3\' => {
                


        
相关标签:
3条回答
  • 2020-12-10 11:34

    The delete operator returns the value being deleted. So this

    $data->{key3}{key4}{key6} = delete $data->{key3}{key4}{key5}
    

    will do what you're looking for.

    0 讨论(0)
  • 2020-12-10 11:50

    You can't replace it, but you can make a new key easily, and then delete() the old one:

    $data->{key3}{key4}{key6} = $data->{key3}{key4}{key5};
    delete $data->{key3}{key4}{key5};
    

    Of course, you could make a fairly simple subroutine to do this. However, my first approach was wrong, and you would need to make a more complex approach that passes in the data structure to modify and the element to be modified, and given that you want elements several levels deep this may be difficult. Though if you don't mind a little clutter:

    sub hash_replace (\%$$) {
      $_[0]->{$_[2]} = delete $_[0]->{$_[1]}; # thanks mobrule!
    }
    

    Then call it:

    hash_replace %{$data->{key3}{key4}}, "key5", "key6";
    

    Or the cool way (How better to say that we're transforming "key5" into "key6" ?):

    hash_replace %{$data->{key3}{key4}}, key5 => "key6";
    

    (Tested and works)

    0 讨论(0)
  • 2020-12-10 11:50

    This 'works' but is very hard-coded.

    #!/bin/perl -w
    use strict;
    
    my $data = {
        'key1' => {
            'key2' => 'value1'
        },
        'key3' => {
            'key4' => {
                'key5' => 'value2'
            }
        },
    };
    
    print "$data->{key3}->{key4}->{key5}\n";
    
    my $save = $data->{key3}->{key4}->{key5};
    delete $data->{key3}->{key4}->{key5};
    $data->{key3}->{key4}->{key6} = $save;
    
    print "$data->{key3}->{key4}->{key6}\n";
    

    You can eliminate the '->' operators between the hash subscripts, but not the one after '$data' - as in Chris Lutz's solution.

    0 讨论(0)
提交回复
热议问题