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

Dear monks,
I created what I think is an array of hash references like this:



push @{$infilestore{$id}}, (split //, $seq);


The %infilestore is meant to hold $id as key and $seq as value.
This seems to work okay, except when I try to open a file and
read in a new file I need infilestore to blank all it's stored so far.


Any ideas ? I tried undef %infilestore, and %infilestore=();
and even @infilestore=(); but it didnt work. Thanks,

basm101

Title edit by tye

  • Comment on undef/re-set an array of hash references ?

Replies are listed 'Best First'.
Re: unde/re-set an array of hash references ?
by rdfield (Priest) on Dec 09, 2002 at 17:38 UTC
    A little more context is needed to answer this fully, but here's a couple of points to ponder:
    • How do you know it didn't work?
    • Are you really, really sure it didn't work?
    • Are you using "strict" and "warnings"?
    • You say "seems to work": have you checked the structure with Data::Dumper just to be sure?

    rdfield

Re: unde/re-set an array of hash references ?
by djantzen (Priest) on Dec 09, 2002 at 17:48 UTC

    I created what I think is an array of hash references like this: push @{$infilestore{$id}}, (split //, $seq);

    What you've got is a hash of array references. Each key ($id) in %infilestore points to a value which is a reference to an array. Thus, in your code above you are dereferencing the array and appending the values you get from splitting $seq

    To clear the hash, you can say undef(%infilestore) or %infilestore = (), or foreach over the keys using delete to clear that entry (not recommended if you just want to get rid of everything though <update>for reasons of efficiency</update>).

      Thanks for the help guys. The hash of array references definition
      makes sense.

      I was trying a foreach approach to clear %infilestore, but when
      I closed an old file (clearing the hash, I hoped) and opened
      a new file, the two would concatenate together - ie. the
      hash would be added to but initial values wern't cleared.

      %infilestore=(); is doing the trick, thanks fever. Why is using foreach
      not recommended if you want to delete everything,
      I would've thought it would do the job fine but apparently not.
      Thanks again,
      basm101
(jeffa) Re: unde/re-set an array of hash references ?
by jeffa (Bishop) on Dec 09, 2002 at 17:45 UTC
    We really need more info, but another thought is to scope your hash so that it will be 'emptied' automatically when the block it is contained in goes out of scope. Something like:
    use strict; use Data::Dumper; my @thing = qw(a c g i t); for my $id (1..5) { my (%infilestore,$seq); $seq .= @thing[rand @thing] for 0..2; push @{$infilestore{$id}}, (split //, $seq); print Dumper \%infilestore; }
    But why empty the hash? Do you have duplicate keys? Please give us more info.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: unde/re-set an array of hash references ?
by pfaut (Priest) on Dec 09, 2002 at 17:33 UTC

    You're trying to empty a hash? Try this.

    %infilestore = ();
Re: unde/re-set an array of hash references ?
by thinker (Parson) on Dec 09, 2002 at 17:48 UTC
    Hi,

    try undef $infilestore.
    You have created a hash_ref, not a hash, but you are trying to clear %infilestore, an unrelated hash.
    Or, perhaps you meant  %$infilestore=undef. That dereferences the hash and undefines it. :-)

    Hope this helps

    thinker

    Update As fever points out, what has been built is a hash of array refs, and not an array of hash refs. This answer does not therefore apply to this question. :-)
      Since he wrote $infilestore{SOMEKEY}, he is indeed working on %infilestore, which is not an unrelated hash at all.

      Makeshifts last the longest.