Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Order in which grep/map receive elements

by abufct (Initiate)
on Oct 05, 2012 at 11:58 UTC ( #997456=note: print w/ replies, xml ) Need Help??


in reply to Re: Order in which grep/map receive elements
in thread Order in which grep/map receive elements

Yes, thank you. I've seen this, but I didn't find any reference to documentation that supports this statement.


Comment on Re^2: Order in which grep/map receive elements
Re^3: Order in which grep/map receive elements
by BrowserUk (Pope) on Oct 05, 2012 at 12:25 UTC

    Probably because it is considered too obvious require documentation.

    Things come out of map/grep in the same order they went in.

    With the single exception of when grep removes item(s) from the stream, in which case those things that do come out, do so in the same relative ordering with which they went in.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      ok, thanks a lot! i've decided to use List::MoreUtils 'uniq', which explicitly states what I want (and uses exactly the same code I've cited in my first post).

        Hm So you'll use the module in preference to the code you posted because it explicitly states ordering, despite knowing that it uses exactly the same code.

        Erm. O-kay.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

        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:

        @array1 = @array2;
        The elements in @array1 will be in the same order has in @array2, right?

        Now do this:

        foreach $element ( @array2 ) { push( @array1, $element ); }
        The elements in @array1 will still be in the same order has in @array2, don't you think?

        And if you do this:

        foreach $element ( @array2 ) { if ( not $seen{$element} ++ ) { push( @array1, $element ); } }
        Guess what? The elements will still be in the same order and I just explicitly did what grep do in one line.

        There are no stupid questions, but there are a lot of inquisitive idiots.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997456]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-07-12 06:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls