SPUG: Fw: Uniq in perl

JALIS,ASIM (HP-FtCollins,ex1) asim_jalis at hp.com
Mon Mar 19 11:10:02 CST 2001


Here is a solution based on hash slices:

  sub uniq { my %hash ; @hash{@_} = 1 ; sort keys %hash ; }

If you dislike the "my" you might prefer:

  sub uniq { @_{@_) = 1 ; sort keys %_ ; }

Asim

-----Original Message-----
From: Brian Ingerson [mailto:BrianI at activestate.com]
Sent: Sunday, March 18, 2001 8:45 PM
To: Richard Anderson
Cc: spug; shivan at ici.net
Subject: Re: SPUG: Fw: Uniq in perl



sub uniq { sort keys %{{ map {($_, 1)} @{$_[0]} }} }

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/


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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