Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>map</strong> is used to create a list by transforming the elements of another list.</p> <p><strong>grep</strong> is used to create a list by filtering elements of another list.</p> <p><strong>sort</strong> is used to create a list by sorting the elements of another list.</p> <p>Each of these operators receives a code block (or an expression) which is used to transform, filter or compare elements of the list.</p> <p>For <strong>map</strong>, the result of the block becomes one (or more) element(s) in the new list. The current element is aliased to $_.</p> <p>For <strong>grep</strong>, the boolean result of the block decides if the element of the original list will be copied into the new list. The current element is aliased to $_.</p> <p>For <strong>sort</strong>, the block receives two elements (aliased to $a and $b) and is expected to return one of -1, 0 or 1, indicating whether $a is greater, equal or less than $b.</p> <p>The <a href="http://en.wikipedia.org/wiki/Schwartzian_transform" rel="noreferrer">Schwartzian Transform</a> uses these operators to efficiently cache values (properties) to be used in sorting a list, especially when computing these properties has a non-trivial cost.</p> <p>It works by creating an intermediate array which has as elements array references with the original element and the computed value by which we want to sort. This array is passed to sort, which compares the already computed values, creating another intermediate array (this one is sorted) which in turn is passed to another map which throws away the cached values, thus restoring the array to its initial list elements (but in the desired order now).</p> <p>Example (creates a list of files in the current directory sorted by the time of their last modification):</p> <pre><code>@file_list = glob('*'); @file_modify_times = map { [ $_, (stat($_))[8] ] } @file_list; @files_sorted_by_mtime = sort { $a-&gt;[1] &lt;=&gt; $b-&gt;[1] } @file_modify_times; @sorted_files = map { $_-&gt;[0] } @files_sorted_by_mtime; </code></pre> <p>By chaining the operators together, no declaration of variables is needed for the intermediate arrays;</p> <pre><code>@sorted_files = map { $_-&gt;[0] } sort { $a-&gt;[1] &lt;=&gt; $b-&gt;[1] } map { [ $_, (stat($_))[8] ] } glob('*'); </code></pre> <p>You can also filter the list before sorting by inserting a <strong>grep</strong> (if you want to filter on the same cached value):</p> <p>Example (a list of the files modified in the last 24 hours sorted the the last modification time):</p> <pre><code> @sorted_files = map { $_-&gt;[0] } sort { $a-&gt;[1] &lt;=&gt; $b-&gt;[1] } grep { $_-&gt;[1] &gt; (time - 24 * 3600 } map { [ $_, (stat($_))[8] ] } glob('*'); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload