Current Issue


Table of contents

CD-ROM

Sys Admin and The Perl Journal CD-ROM version 12.0

Version 12.0 delivers every issue of Sys Admin from 1992 through 2006 and every
issue of The Perl Journal from 1996-2002 in one convenient CD-ROM!

Order now!

Sys Admin Magazine > Archives > 2001 > October 2001
Questions and Answers

Amy Rich
October 2001

Q: I have an array in Perl with a large number of duplicate entries. How do I remove the duplicates?

A: This is actually answered in the perldoc information. Run the following on a system that has Perl installed:

perldoc -q duplicate
And you get the following output:

How can I remove duplicate elements from a list or array?

There are several possible ways, depending on whether the array is ordered and whether you wish to preserve the ordering. (this assumes all true values in the array)

a) If @in is sorted, and you want @out to be sorted:

 $prev = 'nonesuch'; 
 @out = grep($_ ne $prev && ($prev = $_), @in); 

This is nice in that it doesn't use much extra memory, simulating uniq(1)'s behavior of removing only adjacent duplicates. It's less nice in that it won't work with false values like undef, 0, or ""; "0 but true" is OK, though.

b) If you don't know whether @in is sorted:

 undef %saw;  
 @out = grep(!$saw{$_}++, @in); 

c) Like (b), but @in contains only small integers:

 @out = grep(!$saw[$_]++, @in); 

d) A way to do (b) without any loops or greps:

 undef %saw;  
 @saw{@in} = ();  
 @out = sort keys %saw; # remove sort if undesired  
 integers: 

e) Like (d), but @in contains only small positive

 undef @ary;  
 @ary[@in] = @in;  
 @out = grep {defined} @ary; 

But perhaps you should have been using a hash all along, eh?

Q: I'm from a Sun/Solaris background, but my new position is at an HP shop. Is there HP hardware comparable to the Starfire?

A: HP's top of the line machine is the Superdome. In a single node it can have up to 64-way 550 MHz, 4-way PA-8600 CPUs with 1.5




MarketPlace

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.

Six Sigma Certification
100% Online-Six Sigma Certificate from Villanova - Find Out More Now.

Workflow Enabled Help Desk & IT Service Management
Automate service desk activities and integrate processes across IT. Learn more here.

Flowcharts from C/C++ code -- Free trial download
Understand C/C++ code in less time. A new team member ? Inherited legacy code ? Get up to speed faster with Crystal Flow for C/C++. Code-formatting improves readability. Flowcharts are integrated with code browser. Export flowcharts to Visio.

Wanna see your ad here?