dogen has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I'm having a time using a hash reference and wanting
to use the reference to create a new list of hashes to do my dirty work - in essence making a copy of the references for the new list of hashes to use.

while (my $ref = $sth->fetchrow_hashref()) { if ($ref->{multiple}) { foreach my $i (1 .. $number_of_members) { my $new_name = "member" . $i . "_" . $ref->{name}; $ref->{name} = $new_name; $ref->{label} = "Member # $i" . " " . $ref->{label}; push @names, $ref; } } else { push @apps, $ref; } } push @names, @apps;
What I want to do is -depending on the # of members- create a list of hashes (in example @names) that does not reference the original $ref's references but makes a copy for its own use. I'm a little confused on how to copy the contents of the reference instead of directly dealing with the reference itself. In the above example the number of members can vary so I want to give each one its own hash in a new list. But what happens is that instead of getting what I want:
member1_first_name ($ref->{name}) Member #1 First Name ($ref->{label}) member2_last_name Member #2 Last Name ...up to # of members
I get:
member2_member1_first_name Member #2 Member #1 First Name member2_member1_first_name Member #2 Member #1 First Name member2_member1_last_name Member #2 Member #1 Last Name member2_member1_last_name Member #2 Member #1 Last Name
Any suggestions?

update (broquaint): added <code> tags

Replies are listed 'Best First'.
Re: Making a new list of hashes from a hashref
by diotalevi (Canon) on Oct 09, 2003 at 17:45 UTC
    my $new_ref = { %$ref };

    That's a shallow clone of a hash reference. For deep cloning use Storable

      This is the second time today I've seen you advise Storable for performing deep-copies. Nothing wrong with the advice, but I've been using Clone for some time, mostly because it's a fair bit quicker.

      Is there any technical reason you plump for Storable over Clone?


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail

        Uh... because I'd never heard of Clone before. The documentation says it is "less flexible" than Storable's clone but doesn't say anything about what its missing. That's something to keep in mind anyway.

        Oh yeah, and it was one of those copy-paste moments. Two people happened to ask the same question somewhat simultaneously.

        Yes, pointed out, Clone is on the order of twice as fast as Storable.

        In terms of the drawbacks, there have been some reports of core dumps when using Clone on some edge-case structures, although I've forgotten the details (it's certainly not common). Also, Storable provides a hook that lets objects participate in their cloning, so that the copied object can perform some kind of re-initialization or other post-cloning cleanup.

        I've also posted a pure-Perl implementation to CPAN as Clone::PP, which is of course slower than either of the above.

        And for those times where you don't really care how it's implemented, as long as you get a working deep-clone function, I've posted a generic facade called Clone::Any which will attempt to load Storable, Clone, or an equivalent module and will export the clone function from the first one it finds.

      Dio-
      Thanks! thats gave me the result I was Looking for!

      J