in reply to Re: can i rename hashref keys?
in thread can i rename hashref keys?

Given that delete returns the deleted value, this can be simplified:
my %map = (....); $h->{$map{$_}} = delete $h->{$_} for keys %map;

Dave.

Replies are listed 'Best First'.
Re^3: can i rename hashref keys?
by lodin (Hermit) on Nov 29, 2007 at 17:24 UTC

    Given that delete can delete a slice, this can be simplified:

    my %map = (...); @$h{values %map} = delete @$h{keys %map};

    Update: note that this doesn't break on %map = (foo => 'bar', bar => 'foo') that the solutions above may do.

    lodin

      Given that initializing the hash just requires you list a bunch of keys and values, this can be simplified:

      @{$h}{qw/ month year day first_name /}= delete # New keys @{$h}{qw/ mm yyyy dd name /}; # Old keys

      Yeah, I added some extra braces.

      - tye        

Re^3: can i rename hashref keys?
by Anonymous Monk on Nov 29, 2007 at 17:36 UTC
    and even more concisely, using a hash slice:

    @{$h}{qw{month year day first_name}} = delete @{$h}{qw{mm yyyy dd name +}}; C:\@Work\Perl>perl -wMstrict -e "my $h = { mm => 'July', yyyy => '1975', dd => '31', name => 'Milo', l +ast_name => 'Manara',}; print qq(o/p: \n); print qq($_ => $h->{$_} \n) for keys %$h; @{$h}{qw{month year day first_name}} = delete @{$h}{qw{mm yyyy dd name +}}; print qq(-----\n); print qq($_ => $h->{$_} \n) for keys %$h" o/p: mm => July name => Milo dd => 31 last_name => Manara yyyy => 1975 ----- month => July day => 31 first_name => Milo year => 1975 last_name => Manara