[comp.lang.perl] sort -uniq in perl?

ckd@cs.bu.edu (Christopher Davis) (09/08/90)

I am doing a program which works with a dbm file, but doesn't "want" all
the values it stores, so it saves a text file with a sorted array.  The
problem is that I don't see how to get rid of duplicate values (I am using
sort() to combine the file with an array read in at runtime, and there are
often duplicate values).

Is there any way to get sort() to do the equivalent of sort -uniq?  If not,
what's the best way to do it (I assume piping to sort, but I'd like
examples, as I'm still fuzzy on how to get piping to work well)?

I'm using 3.0PL18 on SunOS 4.1.
--
   Christopher Davis, BU SMG '90  <ckd@cs.bu.edu> <...!bu.edu!cs.bu.edu!ckd>
     "Dammit, we're all going to die, let's die doing something *useful*!"
	 --Hal Clement on comments that space exploration is dangerous

ckd@cs.bu.edu (Christopher Davis) (09/08/90)

Well, I went back and hacked this little thing up, but I'm still open to
suggestions for a better way to do it.

sub Uniquify {			# assumes sorted array
    local(@inary) = @_;

    foreach $inline (@inary) {
	if ($inline ne $oldinline) {
	    push(@outary,$inline);
	}
	$oldinline = $inline;
    }
    return @outary;
}
# tear here, my .signature's not valid perl code yet
--
   Christopher Davis, BU SMG '90  <ckd@cs.bu.edu> <...!bu.edu!cs.bu.edu!ckd>
     "Dammit, we're all going to die, let's die doing something *useful*!"
	 --Hal Clement on comments that space exploration is dangerous

raymond@math.berkeley.edu (Raymond Chen) (09/08/90)

In article <CKD.90Sep7213500@bucsf.bu.edu>, 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,,,"));

merlyn@iwarp.intel.com (Randal Schwartz) (09/08/90)

In article <1990Sep8.023805.29123@agate.berkeley.edu>, raymond@math (Raymond Chen) writes:
| In article <CKD.90Sep7213500@bucsf.bu.edu>, 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!"=/