in reply to Re: How can one change the keys of a hash to an alternative set of keys?
in thread How can one change the keys of a hash to an alternative set of keys?

This is an interesting response, because I also had problems with cloning my object. Currently I'm copying every field in the object by hand to the new object.
sub clone { my $self=shift; my $clone=new Board; $clone->{rep}=$self->{rep}; $clone->{castling_rights}=$self->{castling_rights}; $clone->{half_move_clock}=$self->{half_move_clock}; $clone->{full_move_number}=$self->{full_move_number}; $clone->{ep_pos}=$self->{ep_pos}; $clone->{turn}=$self->{turn}; return $clone; }
  • Comment on Re^2: How can one change the keys of a hash to an alternative set of keys?
  • Download Code

Replies are listed 'Best First'.
Re^3: How can one change the keys of a hash to an alternative set of keys?
by AnomalousMonk (Archbishop) on Feb 05, 2015 at 06:15 UTC
    ... copying every field in the object by hand to the new object.

    Maybe (untested):

    use Scalar::Util qw(blessed); sub clone { my $self = shift; my $clone = blessed($self)->new or die "clone failed ($self)"; @{$clone}{keys %$self} = (values %$self); return $clone; }
    Of course, this is still essentially "copying by hand" with lotsa potential pitfalls.


    Give a man a fish:  <%-(-(-(-<

Re^3: How can one change the keys of a hash to an alternative set of keys?
by choroba (Cardinal) on Feb 05, 2015 at 08:57 UTC
    It works only for the listed keys, and also, it only works if the hash is not deeper (no reference to a hash, array, or whatever).
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ