[lug] perl question

Tkil tkil at scrye.com
Tue Jun 4 11:52:46 MDT 2002


>>>>> "jd" == j davis <j> writes:

jd> this little piece of perl removes dups from a list..

this is covered in the docs, especially in perlfaq4:

   http://www.perldoc.com/perl5.6.1/pod/perlfaq4.html#How-can-I-remove-duplicate-elements-from-a-list-or-array-

it's also covered extensively in the perl cookbook.

jd> so they tell me. does anyone here speak extreme laymen
jd> and perl? Please explain how this works, i understand
jd> arrays and hashes and i think i understand $_ just have never
jd> used it. what i dont get is "++$temp{$_} < 2,"

jd>    (this assumes @list has list that needs cleaning)
jd>      %temp = ();
jd>      @list = grep ++$temp{$_} < 2, @list;

someone else has already explained most of this; the key realization
is that $_ is set by "grep" to each value of the list in turn.  the
first time it sees a particular value, say "foo", $temp{foo} is
created with the undef value, and pre-incremented to 1.  since this is
less than 2, the expression returns true, and the first occurance of
"foo" is returned from the grep.  the second time it sees "foo",
$temp{foo} is pre-incremented to 2, which is not less than 2, so it
fails, and the second (and subsequent) occurances of "foo" are not
passed on as the return value.

it's instructive to dump the contents of %temp after using it in this
fashion.  (i'd personally wrap that in a local scope, to keep %temp
from being visible elsewhere.)  consider:

| my @list = qw( a b c d e a b q p i b a a e );
| 
| print "pre:  ( @list )\n";
| 
| {
|    my %temp;
|    @list = grep { ++$temp{$_} < 2 } @list;
|    print "temp: ( ", join(", ", map "$_ => $temp{$_}", keys %temp), " )\n";
| }
| 
| print "post: ( @list )\n";

which prints out:

| pre: ( a b c d e a b q p i b a a e )
| temp: ( e => 2, p => 1, i => 1, q => 1, a => 4, b => 3, c => 1, d => 1 )
| post: ( a b c d e q p i )

in other words, %temp is a histogram of @list; as we build it up, we
use "grep" to only grab list members when the histogram count is 1.

t.



More information about the LUG mailing list