SPUG: Fw: Uniq in perl

jeff saenz jeff at planetoid.net
Mon Mar 19 14:35:47 CST 2001


What if you convert the array values to hash keys and let the hash eliminate the
duplicates? Something like:

@hash{@array} = ();
or maybe a map. but i think maps are inefficient.  something like that.

Richard Anderson wrote:

> Does anyone have comments for Mike?
>
> Richard Anderson, Ph.D.          www.unixscripts.com
> Perl / Oracle / Unix                Richard.Anderson at raycosoft.com
> Raycosoft, LLC                        Seattle, WA, USA
> ----- Original Message -----
> From: "Mike" <shivan at ici.net>
> To: "Richard Anderson" <Richard.Anderson at raycosoft.com>
> Sent: Friday, March 16, 2001 2:12 PM
> Subject: Uniq in perl
>
> > This is something I had sitting around. I didn't write it. Its a subroutine
> > that uniqs an array. I was just wondering if this is the best way to do it
> > or if anyone has any thing better. Thanks
> >
> >
> > ## Usage: &uniq( \@ARRAY );
> > sub uniq {
> > my (@uwork, @unew, $uname);
> >    @uwork = @{$_[0]};
> >    @uwork = sort( @uwork );
> >    @unew  = ( shift @uwork );
> >
> >    foreach $uname ( @uwork ) {
> >       @unew=( @unew, $uname) if ( $uname ne $unew[ -1 ] );
> >    }
> >
> >    @{$_[0]}=@unew;
> > }
> >
> > :wq
> >
> >
>
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>      POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
>       Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
>   Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
>  For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
>   Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list