<?xml version="1.0" encoding="windows-1252"?>
<node id="997479" title="Re^5: Order in which grep/map receive elements" created="2012-10-05 09:55:44" updated="2012-10-05 09:55:44">
<type id="11">
note</type>
<author id="823564">
greengaroo</author>
<data>
<field name="doctext">
&lt;p&gt;BrowserUk is right, don't use a module to do that if you can do it with one line of code! Grep receives &lt;del&gt;an array&lt;/del&gt; a LIST and returns &lt;del&gt;an array&lt;/del&gt; a LIST. The returned array will be shorter, but the elements will be added in the order in which they are encountered in the other array.&lt;/p&gt;
&lt;p&gt;Imagine you do this: 
&lt;code&gt;
@array1 = @array2;
&lt;/code&gt;
The elements in @array1 will be in the same order has in @array2, right?
&lt;/p&gt;
&lt;p&gt;Now do this: 
&lt;code&gt;
foreach $element ( @array2 ) {

  push( @array1, $element );
}
&lt;/code&gt;
The elements in @array1 will still be in the same order has in @array2, don't you think?
&lt;/p&gt;

&lt;p&gt;And if you do this: 
&lt;code&gt;
foreach $element ( @array2 ) {

  if ( not $seen{$element} ++ ) {
    push( @array1, $element );
  }
}
&lt;/code&gt;
Guess what? The elements will still be in the same order and I just explicitly did what grep do in one line.
&lt;/p&gt;


&lt;div class="pmsig"&gt;&lt;div class="pmsig-823564"&gt;
&lt;div class="pmsig"&gt;
  &lt;i&gt;There are no stupid questions, but there are a lot of inquisitive idiots.&lt;/i&gt;&lt;br/&gt;
&lt;div class="pmsig-823564"&gt;
  -- [id://823564]
&lt;/div&gt;&lt;/div&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
997453</field>
<field name="parent_node">
997466</field>
</data>
</node>
