my $hh = %hash

T

TechCrazy

I have copy of Perl Little Black Book which does not talk about the
following. So, I am asking this question here:

For,
my $hh = %hash;

%hash is a hash of hashes. Would the statement above
1) create a copy of %hash and assign it to hh
OR
2) only a reference is created.

Thanks.
 
E

Eric Schwartz

TechCrazy said:
I have copy of Perl Little Black Book which does not talk about the
following. So, I am asking this question here:

For,
my $hh = %hash;

%hash is a hash of hashes. Would the statement above
1) create a copy of %hash and assign it to hh

You don't have a 'hh' variable, you have '$hh'. So this is
impossible. For your statement above to be true, you'd have to write
something like:

my %hh = %hash;

Note the important '%' instead of '$'.
OR
2) only a reference is created.

You only get a reference when you explicitly ask for one, which you
didn't do there. If you read 'perldoc perldata', you will see:

If you evaluate a hash in scalar context, it returns false if
the hash is empty. If there are any key/value pairs, it
returns true; more pre- cisely, the value returned is a string
consisting of the number of used buckets and the number of
allocated buckets, separated by a slash.

This documentation is included in your Perl distribution, and people
around here get somewhat testy in general when asked to read to
someone the documentation already on their own system. To avoid this
sort of reaction, I strongly recommend you familiarize yourself with
the contents of the FAQ ('perldoc perlfaq'), and at least enough of
the rest of the documentation ('perldoc perltoc') to know where to
look up the answers yourself first, before asking others.

Oh yeah, another question you'll see around here a lot when you ask a
question like that one: What happened when you tried it? You've got a
Perl interpreter on your machine (if not, then install one), so why
ask several hundred people to do for you something you can easily ask
Perl to do in a few nanoseconds, tops?

-=Eric
 
T

TechCrazy

Oops, I actually meant

my %hh = %hash ;

But, thanks a lot for pointing me to the documentation. I didn't know
these sources.
 
P

peter pilsl

You can do:

my $hh = { %hash };

to create a new copy.


Note that copy might not be the correct term here. The OP states that
%hash is a hash of hashes, which means, that it is a hash, that contains
references to hashes.
So the above statement creates a copy of this references, but these
references still point to the same "anonymous" hashes.

%hash=(a=>{b=>4});
%hh=(%hash);
$hh{a}->{b}=5;
print $hash{a}->{b},"\n";

the result is 5 which might no be what the OP wants.

For cloning the whole structure one needs to use a recursive attempt
like it is provided by some modules from CPAN (dont know the name) or
can easily (at least its a good training in references and recursions)
implemented by yourself.

best,
peter
 
V

vali

peter said:
Note that copy might not be the correct term here. The OP states that
%hash is a hash of hashes, which means, that it is a hash, that contains
references to hashes.
So the above statement creates a copy of this references, but these
references still point to the same "anonymous" hashes.

%hash=(a=>{b=>4});
%hh=(%hash);
$hh{a}->{b}=5;
print $hash{a}->{b},"\n";

the result is 5 which might no be what the OP wants.

For cloning the whole structure one needs to use a recursive attempt
like it is provided by some modules from CPAN (dont know the name) or

use Storable qw(dclone);
%new_hash = %{ dclone(\%hash) };

__Vali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top