Strange Behaviour with Hash???

F

Fergus Toolan

Hi all,
We're currently writing some scripts that deal with hashs and are
getting some strange behaviour with them. We have very little experience
with dealing with hashs so I want to check something.

Is it correct to assign one hash to another? For instance

%H1 = %H2;

I want the hashs to be distinct. Will the above statement completely
overwrite the current contents of %H1?

Any comments would be much appreciated
Regards
Fergus
 
G

Gunnar Hjalmarsson

Fergus said:
Is it correct to assign one hash to another? For instance

%H1 = %H2;
Yes.

I want the hashs to be distinct. Will the above statement
completely overwrite the current contents of %H1?

Yes.
 
P

Paul Lalli

Hi all,
We're currently writing some scripts that deal with hashs and are
getting some strange behaviour with them. We have very little experience
with dealing with hashs so I want to check something.

Is it correct to assign one hash to another?

'correct' is a relative term. It depends on what you want to do.
For instance

%H1 = %H2;

I want the hashs to be distinct.

They will not necessarily be completely individual entities. If %H2's
values are references to other data (as in multi-level data structures
like hash of hashes or hash of arrays, or even just references to other
scalar data), those references will be copied. The data will not. For
example, if $H2{'arr'} = \@array, then after the assignment, $H1{'arr'}
will also be a reference to @array. Any changes you make to @{$H1{'arr'}}
will be reflected in @{$H2{'arr'}}.

There are a couple ways of creating fully independent structures. The one
I'm remembering right now is via a module called Clone. Look for it at
search.cpan.org
Will the above statement completely overwrite the current contents of %H1?

Yes. Any prior contents of %H1 are gone from that hash, and cannot be
accessed throw $H1{'(anything)'}.
Any comments would be much appreciated

Perhaps if you told us exactly what the strange behavior was that you're
seeing, we could help you more effectively diagnose the problem.

Paul Lalli
 
B

Brad Baxter

Hi all,
We're currently writing some scripts that deal with hashs and are
getting some strange behaviour with them. We have very little experience
with dealing with hashs so I want to check something.

Is it correct to assign one hash to another? For instance

%H1 = %H2;

I want the hashs to be distinct. Will the above statement completely
overwrite the current contents of %H1?

Just curious--what's the strange behaviour?

Regards,

Brad
 
F

Fergus Toolan

Hi all,
Thanks to everyone who has replyed so far.
Just to describe it a bit more.
Both H1 and H2 are hashs of arrays. I know that when I do the
assignement that the references will be the same but this desired.

We initialise %H1 with values read in from a file. This step is working
fine.
We begin a loop at this stage for k iterations.
For each value in H1 we perform some tests (a bitwise comparison between
the arrays). Depending on the result of this we assign the hash element
i.e. the array ref to H2. when we are finished this process we then
perform the assignment i.e.

%H1 = %H2

We seem to be able to access keys in H1 that were there before the
assignment but that are not in H2.
However judging from the responses so far our problem is not with the
assignment itself but it most be something buried in the logic of our
creation of H2.

Oh well back to the toil :)
Thanks again for your help
Regards
Fergus
 
P

Paul Lalli

Hi all,
Thanks to everyone who has replyed so far.

You're welcome.
Just to describe it a bit more.
Both H1 and H2 are hashs of arrays. I know that when I do the
assignement that the references will be the same but this desired.

I find that mildly unlikely, but I'll take your word for it for now.
We initialise %H1 with values read in from a file. This step is working
fine.
We begin a loop at this stage for k iterations.
For each value in H1 we perform some tests (a bitwise comparison between
the arrays).
Depending on the result of this we assign the hash element
i.e. the array ref to H2.

Here's where you lose me. %H2 is a hash. How are you assigning an array
ref to a hash? Your wording makes no sense.
when we are finished this process we then
perform the assignment i.e.

%H1 = %H2

Confused again. If you're first assigning pieces of H1 to H2, how/why are
you then assigning all of H1 to H2?

You should really really post a *runnable* snippet of code that
illustrates your problem. As the posting guidelines suggest, "Speak Perl,
not English".

Paul Lalli

P.S. In the future, please observe another statement in the posting
guidelines - post your replies *below* the original material, with the
quoted text on top. This makes everyone happier. Thanks.
 
C

ctcgag

Paul Lalli said:
Confused again. If you're first assigning pieces of H1 to H2, how/why
are you then assigning all of H1 to H2?

I assume you mean "all of H2 to H1".
This sounds like the classical task of transactionally updating a data
structure. You can't delete the "goners" from H1 immediately as you
recgonize them while iterating over H1, because they need to be used in
comparisons of other things that are still to be checked. So you save the
ones you want to keep to a temporary structure, then use that temporary
structure to replace the original when you are done.

If this is the case, the OP probably shouldn't use a second hash at all,
but an array.

Something like:

my @goner;
while (my $k=each %H1) {
push @goner, $k unless want_to_keep($k);
};
delete @H1{@goner};

Xho
 
V

vali

Fergus said:
Hi all,
We're currently writing some scripts that deal with hashs and are
getting some strange behaviour with them. We have very little
experience with dealing with hashs so I want to check something.

Is it correct to assign one hash to another? For instance

%H1 = %H2;

I want the hashs to be distinct. Will the above statement completely
overwrite the current contents of %H1?

Any comments would be much appreciated
Regards
Fergus
You may want:

use Storable qw(dclone);
my %H1 = %{ dclone(\%H2)};
 
A

Anno Siegel

Fergus Toolan said:
Hi all,
Thanks to everyone who has replyed so far.
Just to describe it a bit more.
Both H1 and H2 are hashs of arrays. I know that when I do the
assignement that the references will be the same but this desired.
[...]

We seem to be able to access keys in H1 that were there before the
assignment but that are not in H2.
However judging from the responses so far our problem is not with the
assignment itself but it most be something buried in the logic of our
creation of H2.

Oh well back to the toil :)

I suspect autovivification of hash keys during the buildup of H2. This
is nothing but a WAG -- if there are also unwanted *values* in the
resulting hash, autovivification is not the cause. You mention only
unwanted keys with, presumably, undefined values, which would be
consistent with autovivification.

Anno
 
J

J. Romano

vali said:
You may want:

use Storable qw(dclone);
my %H1 = %{ dclone(\%H2)};

Dear Fergus,

The line "%H1 = %H2;" will only create %H1 as its own distinct hash
as long at %H2 contains no references. But since you already stated
in a previous post that %H2 is a hash of array references, that won't
do what you want.

In your case, you can use the advice in vali's reply of using the
Storable module. However, I've found that not all Perl installations
have the Storable module (and I don't have permissions to install
modules from CPAN, either). If this is the case for you, the
Data::Dumper module (which should be a standard module) works great in
the absence of the Storable module:

use Data::Dumper;
%H1 = %{ eval Dumper(\%H2) }; # copies a hash with references
@A1 = @{ eval Dumper(\@A2) }; # copies an array with references
$a = eval Dumper($b); # copies an object (reference)

I like to use the Data::Dumper module to copy complex hashes and
arrays because I find that more portable, considering that all Perl
installations I've encountered have Data::Dumper. But if you already
have the Storable module (or don't mind installing it from CPAN), you
might as well use vali's approach over mine.

-- Jean-Luc
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top