Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Uniq, anyone? Message-ID: <9952@jpl-devvax.JPL.NASA.GOV> Date: 12 Oct 90 23:09:08 GMT References: <1990Oct12.075314.15054@utgard.uucp> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Distribution: na Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 50 In article <1990Oct12.075314.15054@utgard.uucp> chris@utgard.uucp (Chris Anderson) writes: : On occasion, I find myself wanting something on the order of: : : @foo = (1,1,2,2,3,3); : @bar = &uniq (@foo); : : Where @bar contains: 1 2 3 : : I've made a couple of stabs at coding this and haven't come : up with anything that's very efficient. Has anyone made such : a beast? @in = (1,1,2,2,3,3); # If we know that @in is sorted: $prev = 'nonesuch'; @out = grep($_ ne $prev && (($prev) = $_), @in); print "@out\n"; # If we don't know whether @in is sorted: undef %saw; @out = grep(!$saw{$_}++, @in); print "@out\n"; # If you don't know about @in, and don't care whether @out is sorted: undef %ary; @ary{@in} = (); @out = keys(%ary); print "@out\n"; # Same as above, but assuming that all values of @in are small positive ints. $prev = 'nonesuch'; @out = grep($_ != $prev && (($prev) = $_), @in); print "@out\n"; undef @saw; @out = grep(!$saw[$_]++, @in); print "@out\n"; undef @ary; @ary[@in] = @in; @out = sort @ary; print "@out\n"; Larry