Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!sdd.hp.com!decwrl!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: sort -uniq in perl? Message-ID: <1990Sep8.051241.6192@iwarp.intel.com> Date: 8 Sep 90 05:12:41 GMT References: <1990Sep8.023805.29123@agate.berkeley.edu> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 29 In-Reply-To: raymond@math.berkeley.edu (Raymond Chen) In article <1990Sep8.023805.29123@agate.berkeley.edu>, raymond@math (Raymond Chen) writes: | In article , ckd@cs (Christopher Davis) writes: | >Well, I went back and hacked this little thing up, but I'm still open to | >suggestions for a better way to do it. | | Howzabout a one-liner? (Though Randal can probably shorten it...) | | # Teaching Uniquify to handle null strings is no big deal, but | # it sort of spoils the cuteness of the line-liner... | | sub Uniquify{local($prev);grep($_ ne$prev&&($prev = $_),@_);} | | print &Uniquify(split(//,"JJJJusstt aannothhher ppperlll haaacker,,,")); Arggghh! O(n**2) algorithms are for the birds. Remembering that the original request was "sort -u" equivalent, I flip to page 29 of "the book", and type in: undef %mark; grep($mark{$_}++,@old); @new = sort keys %mark; undef %mark; All the goodies in here you won't believe, and we're still typin'em in. print "Just another Perl [book] 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!"=/