Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Order in which grep/map receive elements

by abufct (Initiate)
on Oct 05, 2012 at 11:48 UTC ( #997453=perlquestion: print w/ replies, xml ) Need Help??
abufct has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I am trying to solve a well-known problem of extracting unique values from array. I am using the following code for this:
my @set = qw(a b c a c); my %seen; my @unique = grep { not $seen{$_} ++ } @set; # http://www.perlmonks.org/?node_id=614322
The problem is that it is important to preserve the order in which elements appeared in the original array. So my question: is it guaranteed that grep will execute code blocks for each element from @set in order? I could not find that this is directly stated in the docs.

Comment on Order in which grep/map receive elements
Download Code
Re: Order in which grep/map receive elements
by BrowserUk (Pope) on Oct 05, 2012 at 11:54 UTC
    I could not find that this is directly stated ...

    You didn't look very hard. In the third reply to the post you quote it say "That method preserves the order of the original array ".


    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

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

        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

Re: Order in which grep/map receive elements
by tobyink (Abbot) on Oct 05, 2012 at 14:54 UTC

    You are correct that this is not stated in the documentation, but it has always been the case, and there is a large amount of code that relies on it, so it can't really change in future Perls without breaking massive amounts of code.

    One thing I could kind of see happening in a future version of Perl might be to have map/grep fork and run the block in parallel on each element, but still return the results in order. This would still break some code, but if it were enabled only by a particular lexical pragma, then it could work.

    But anyway, the current behaviour of map/grep being preserved is not something you ought to worry about.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      thanks! that's what I wanted to know. and yes, my biggest concern is about parallelizing grep. it would definitely break my code. but since you say that lot of people and code rely on grep's consistency, i believe it is some kind of guarantee.
        Another way to look at it is that unlike e.g. each, the docs for map and grep do not say anything about it not returning values in order (not to mention that the examples assume it).

        Also, looking at the pitfalls of emulating fork on Windows and possible "thread safe" concerns, I would hope that any parallelization would be introduced in some optional way, such as a new function (e.g. pgrep), or using pragmas to turn it on or off. Or worst case, even if none of that occurred, I would be shocked if there was not a way to compile perl without a parallelized grep.

        Elda Taluta; Sarks Sark; Ark Arks
        My deviantART gallery

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-09-23 19:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (239 votes), past polls