On Jan 18, 2008 11:57 AM, Kevin Viel <[EMAIL PROTECTED]> wrote:
> Is there a way to empty/clear a hash in mass?
>
> For instance:
>
> %hash = {} ;
>
>
> Might the above create an reference?
snip

The above does create a reference.  In fact, the hash will now contain
something like

%hash = (
    "HASH(0x1801180)" => undef
)

You initialize a hash with a list.  So if you want an empty hash then
you need to assign an empty list to it:

%hash = ();

However, this is a red flag that you are probably doing something
wrong.  Most of the time it means you have declared your hash with too
large of a scope (generally due to contamination by ANSI C memes like
declaring all of your variables at the top of a function):


my %hash;
while (<>) {
    #clean out old hash
    %hash = ()
    #make a hash set of the words in the line
    $hash{$_} = 1 for split;
    #do something else with %hash
}

Should be written

while (<>) {
    my %hash;
    #make a hash set of the words in the line
    $hash{$_} = 1 for split;
    #do something else with %hash
}

or better yet

while (<>) {
    #make a hash set of the words in the line
    my %hash = map { $_ => 1 } split;
    #do something else with %hash
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to