From 3bc5ef3eb693992b74dc57b15c2a80e4e456a97e Mon Sep 17 00:00:00 2001 From: Hans Ginzel Date: Fri, 27 Oct 2000 21:28:30 +0200 Subject: [ID 20001027.007] uniq array in perlfaq Message-Id: <20001027192830.A1564@kolej.mff.cuni.cz> p4raw-id: //depot/perl@7474 --- pod/perlfaq4.pod | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'pod/perlfaq4.pod') diff --git a/pod/perlfaq4.pod b/pod/perlfaq4.pod index 79905f88ce..9d6b766a9f 100644 --- a/pod/perlfaq4.pod +++ b/pod/perlfaq4.pod @@ -952,12 +952,12 @@ ordered and whether you wish to preserve the ordering. (this assumes all true values in the array) $prev = 'nonesuch'; - @out = grep($_ ne $prev && ($prev = $_), @in); + @out = grep($_ ne $prev && ($prev = $_, 1), @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. +uniq(1)'s behavior of removing only adjacent duplicates. The ", 1" +guarantees that the expression is true (so that grep picks it up) +even if the $_ is 0, "", or undef. =item b) If you don't know whether @in is sorted: -- cgit v1.2.1