Strange Behaviour with Hash???

Discussion in 'Perl Misc' started by Fergus Toolan, Jun 24, 2004.

  1. 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
     
    Fergus Toolan, Jun 24, 2004
    #1
    1. Advertising

  2. Fergus Toolan wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 24, 2004
    #2
    1. Advertising

  3. Fergus Toolan

    Paul Lalli Guest

    On Thu, 24 Jun 2004, Fergus Toolan wrote:

    > 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
     
    Paul Lalli, Jun 24, 2004
    #3
  4. Fergus Toolan

    Brad Baxter Guest

    On Thu, 24 Jun 2004, Fergus Toolan wrote:

    > 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
     
    Brad Baxter, Jun 24, 2004
    #4
  5. 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



    Fergus Toolan wrote:
    > 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
    >
     
    Fergus Toolan, Jun 24, 2004
    #5
  6. Fergus Toolan

    Paul Lalli Guest

    On Thu, 24 Jun 2004, Fergus Toolan wrote:

    > Fergus Toolan wrote:
    > > 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?
    > >

    > 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.
     
    Paul Lalli, Jun 24, 2004
    #6
  7. Fergus Toolan

    Guest

    Paul Lalli <> wrote:
    >
    > > 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?


    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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 25, 2004
    #7
  8. Fergus Toolan

    vali Guest

    Fergus Toolan wrote:

    > 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)};
     
    vali, Jun 25, 2004
    #8
  9. Fergus Toolan

    Anno Siegel Guest

    Fergus Toolan <> wrote in comp.lang.perl.misc:
    > 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
     
    Anno Siegel, Jun 28, 2004
    #9
  10. Fergus Toolan

    J. Romano Guest

    > Fergus Toolan wrote:
    > > 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?


    vali <> replied:
    > 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
     
    J. Romano, Jun 29, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Chambers
    Replies:
    1
    Views:
    392
    Daniel Dyer
    May 14, 2007
  2. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
  3. Kev Jackson

    Odd hash behaviour

    Kev Jackson, Oct 4, 2005, in forum: Ruby
    Replies:
    7
    Views:
    129
    Gavin Kistner
    Oct 4, 2005
  4. Alexander Presber
    Replies:
    50
    Views:
    472
    Trans
    Jun 25, 2007
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    629
    David A. Black
    Jul 2, 2008
Loading...

Share This Page