[comp.lang.perl] Removing associative array entries

jimmy@pyra.co.uk (Jimmy Aitken) (04/09/91)

I have a list of stuff in an associative array, and as I process each
elemnt, I want to undefine it.  When I finish processing the array via
a key look up, the element is removed. This is done so that I have the
remaining entries left which need to be handled specially.  i.e.

while(<>) {
    last if /^end/;
    $ARRAY{$_}=1;
}

while(<>) {
    ($f1,$f2,$f3)=split(/"/,$_);
    if (defined($ARRAY{$f2})) {
	process it;
	undef $ARRAY{$f2};
    }
}

while(($key,$value)=each%ARRAY) {
    process $value;
}

In the case above, the entry in the array ($value) is undefined, but
the name of the element is still there.  Is there any way to remove
the name of the element sso it wont appear in the $key variable in the
above example?

I can get around the problem, by  doing a 'prcoess $key if
defined($value)', but I was wondering if there was a 'cleaner' way.

Thanks,

Jimmy
--
jimmy@pyra.co.uk                 -m-------  Jimmy Aitken                
jimmy@pyramid.pyramid.com      ---mmm-----  Pyramid Technology Ltd
..!mcsun!ukc!pyrltd!jimmy   -----mmmmm---  Pyramid House, Solartron Rd
(+44) 252 373035           -------mmmmmmm-  Hants GU14 7PL, ENGLAND
 - Managing software engineers is like herding cats

tchrist@convex.COM (Tom Christiansen) (04/10/91)

From the keyboard of jimmy@pyra.co.uk (Jimmy Aitken):
:In the case above, the entry in the array ($value) is undefined, but
:the name of the element is still there.  Is there any way to remove
:the name of the element sso it wont appear in the $key variable in the
:above example?

Use the delete operator.

    delete $a{$x};

--tom