Path: utzoo!attcan!uunet!snorkelwacker!apple!rutgers!mcnc!thorin!oscar!tell From: tell@oscar.cs.unc.edu (Stephen Tell) Newsgroups: comp.lang.perl Subject: Re: Best way to nullify an intersection Message-ID: <14971@thorin.cs.unc.edu> Date: 3 Jul 90 14:07:54 GMT References: <601@inpnms.ROCKVILLE.DG.COM> Sender: news@thorin.cs.unc.edu Reply-To: tell@oscar.cs.unc.edu (Stephen Tell) Organization: University Of North Carolina, Chapel Hill Lines: 62 In article <601@inpnms.ROCKVILLE.DG.COM> logan@rockville.dg.com (James L. Logan) writes: >Is there a better or faster way to drop all elements in set B that >intersect with set A? Here's what I've got so far: > open(FILE1, "file1") || die; > @A = ; > close(FILE1) || die; > > open(FILE2, "file2") || die; > @B = ; > close(FILE2) || die; > > foreach $element (@A) { > foreach (@B) { > if (/^$element$/) { > $_ = ''; > last; > } > } > } > print @B; >Also, how can I delete the element that was matched in @B, while >also shortening the array by one element; rather than just >setting the element to the null string? Its called splice(@array, offset, length, [replace]) "Delete length elements from @array starting at offset and return them, optionaly replacing them with new elements." > Thanks, > -Jim This is my first comp.lang.perl posting with a coding suggestion, so be gentle, guys! It seems that using an associative array to say "did we see this anywhere" would help a lot over the O(n**2) approach above. You said these are sets, if the order in which B gets printed matters, this goes out the window. But try: open(FILE1, "file1") || die; @A=; close(FILE1) || die; open(FILE2, "file2") || die; while() { $B{$_} = 1; } close(FILE2) || die; foreach $element (@A) { if ($B{$element} { delete($B{$element}); } } print keys(%B); Is there a better way to read FILE2 into an associative array like this? Perhaps the ability to say keys(%B) = ; -------------------------------------------------------------------- Steve Tell e-mail: tell@wsmail.cs.unc.edu usmail: #5L Estes Park apts CS Grad Student, UNC Chapel Hill. Carrboro NC 27510