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

If I have the following hash: my %hash=(a=>1,b=>2); I can initialize/clear out the hash by doing: %hash=(); So if I pass a hash to a sub as a reference, is there a way to initialize/clear the hash using the reference?
my %hash=(a=>1,b=>2); my $x=&foo(\%hash); sub foo{ my $h=shift; #hash reference #I want to clear out the hash here... $h->{c}=5; return 1; }

Replies are listed 'Best First'.
Re: Can I initialize/clear a hash reference?
by Zaxo (Archbishop) on Feb 25, 2005 at 04:51 UTC

    sub foo { my $h = shift; %$h = (); $h->{'c'} = 5; 1; }

    After Compline,
    Zaxo

Re: Can I initialize/clear a hash reference?
by davido (Cardinal) on Feb 25, 2005 at 04:52 UTC

    This will do it:

    sub foo { my $href = shift; %{$href} = (); $href->{c} = 5; return 1; }

    You just have to dereference the hashref as an entire hash entity.


    Dave

      My thanks to both of you who replied.