<?xml version="1.0" encoding="windows-1252"?>
<node id="280658" title="Perl Idioms Explained - keys %{{map{$_=&gt;1}@list}}" created="2003-08-04 08:55:41" updated="2005-08-15 11:49:11">
<type id="120">
perlmeditation</type>
<author id="87452">
broquaint</author>
<data>
<field name="doctext">
&lt;b&gt;Perl Idioms Explained - &lt;tt&gt;keys %{{map{$_=&gt;1}@list}}&lt;/tt&gt;&lt;/b&gt;
&lt;p/&gt;

&lt;code&gt;
my @list = qw/a b c d d a e b a b d e f/;
my @uniq = keys %{{ map {$_ =&gt; 1} @list }};

print "@list\n@uniq\n";

__output__

a b c d d a e b a b d e f
e f a b c d
&lt;/code&gt;

The above idiom is a simple way of creating a list of unique values from another
list, as the output of the code aptly demonstrates. However, with all those
curly braces it may not be immediately obvious what's going on, so let's break
it down.

&lt;blockquote&gt;&lt;code&gt;
map { $_ =&gt; 1 } @list
&lt;/code&gt;&lt;/blockquote&gt;

This is pretty straight-forward - create a list of key-value pairs where the keys
are the values from &lt;tt&gt;@list&lt;/tt&gt;.

&lt;blockquote&gt;&lt;code&gt;
{ map { $_ =&gt; 1 } @list }
&lt;/code&gt;&lt;/blockquote&gt;

The surrounding pair of curly braces creates an anonymous hash which is
populated with they key-value pairs from the &lt;tt&gt;[perlfunc:map|map]&lt;/tt&gt;
statement. So we now have a hash reference to an anonymous hash who's keys are the elements from &lt;tt&gt;@list&lt;/tt&gt;, and because hash keys are unique, the keys of the anonymous hash represent a unique set of values.

&lt;blockquote&gt;&lt;code&gt;
keys %{ { map { $_ =&gt; 1 } @list } }
&lt;/code&gt;&lt;/blockquote&gt;

Finally, with the last pair of curly braces the hash reference to the anonymous hash is dereferenced and we get its list of keys.

&lt;p/&gt;
&lt;b&gt;Caveats&lt;/b&gt;
&lt;p/&gt;

Because this idiom creates a list, and then a hash, both of which are
immediately disposed of, it's not suited for large lists. Also, because the keys
of a hash aren't ordered, the list of unique values returned will be in a random
order (although this can often be remedied with a simple
&lt;tt&gt;[perlfunc:sort|sort]&lt;/tt&gt;).

&lt;p/&gt;
&lt;b&gt;Summary&lt;/b&gt;
&lt;p/&gt;

A short (memory hungry) way to get a list of unique values from a given list.

&lt;p/&gt;
&lt;tt&gt;_________&lt;br&gt;&lt;u&gt;broquaint&lt;/u&gt;&lt;/tt&gt;</field>
</data>
</node>
