Path: utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!sdd.hp.com!ucsd!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: Best way to nullify an intersection Message-ID: <1990Jul3.170621.28795@iwarp.intel.com> Date: 3 Jul 90 17:06:21 GMT References: <601@inpnms.ROCKVILLE.DG.COM> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 48 In-Reply-To: logan@rockville.dg.com (James L. Logan) In article <601@inpnms.ROCKVILLE.DG.COM>, logan@rockville (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? Well, I'd attack it something like this: open(F1,"file1") || die "cannot open file1: $!"; while () { $saw{"$_"}++; } close(F1); open(F2,"file2") || die "cannot open file2: $!"; while () { print unless $saw{$_}; } close(F2); Using the keys of an assoc array as a set-type is a very handy idiom. print "Just another Perl hacker," -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/