in reply to Re^4: Order in which grep/map receive elements
in thread Order in which grep/map receive elements
BrowserUk is right, don't use a module to do that if you can do it with one line of code! Grep receives an array a LIST and returns an array 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.
Imagine you do this:
The elements in @array1 will be in the same order has in @array2, right?@array1 = @array2;
Now do this:
The elements in @array1 will still be in the same order has in @array2, don't you think?foreach $element ( @array2 ) { push( @array1, $element ); }
And if you do this:
Guess what? The elements will still be in the same order and I just explicitly did what grep do in one line.foreach $element ( @array2 ) { if ( not $seen{$element} ++ ) { push( @array1, $element ); } }
There are no stupid questions, but there are a lot of inquisitive idiots.
-- greengaroo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Order in which grep/map receive elements
by jwkrahn (Abbot) on Oct 06, 2012 at 06:26 UTC | |
by greengaroo (Hermit) on Oct 09, 2012 at 18:44 UTC | |
Re^6: Order in which grep/map receive elements
by abufct (Initiate) on Oct 06, 2012 at 01:22 UTC | |
by Tanktalus (Canon) on Oct 06, 2012 at 04:11 UTC |
In Section
Seekers of Perl Wisdom