On Tue, Mar 13, 2001 at 11:37:31PM -0500, Adam Stern wrote:
>
> I don't think I can do a straight forward copy %store = %master. Why?
> Because I'm using references to arrays, not actual arrays. So the values
> in %store point to the same things as %master. If %master changes, so
> does %store.
perlfaq4:
How do I print out or copy a recursive data structure?
The Data::Dumper module on CPAN (or the 5.005 release of Perl) is great
for printing out data structures. The Storable module, found on CPAN,
provides a function called C<dclone> that recursively copies its argument.
use Storable qw(dclone);
$r2 = dclone($r1);
Where $r1 can be a reference to any kind of data structure you'd like.
It will be deeply copied. Because C<dclone> takes and returns references,
you'd have to add extra punctuation if you had a hash of arrays that
you wanted to copy.
%newhash = %{ dclone(\%oldhash) };
> But, then again, when I try the %store = %master thing in my program:
>
> for (1..$times) {
> %orig = %master; # master changes each time the
> # for loop is executed (sometimes)
> print "processing time number $_\n";
> process();
> %new = %master;
> if (%new != %orig) { print "***changed\n"; }
> }
>
> It never says "***changed", I think because I am using refs.
In a scalar context, a hash returns a string which looks like "7/16",
where the second number is how many buckets have been allocated for the
hash, and the first is how many of those buckets are in use.
To compare two hashes, you could stringify them and compare the results.
Be aware that the method you use to stringify must process the keys in
sorted order; I don't think Data::Dumper does this. An alternate module,
Data::Dump, does.
Ronald