What's the difference between grep and map in Perl?

前端 未结 6 779
逝去的感伤
逝去的感伤 2020-12-31 06:50

In Perl both grep and map take an expression and a list, and evaluate the expression for each element of the list.

What is the difference b

6条回答
  •  旧时难觅i
    2020-12-31 07:05

    I find that it's helpful to think think about grep() and map() in their most general form:

    grep {BLOCK} LIST   
    map  {BLOCK} LIST
    

    grep() is a filter: it returns the subset of items from LIST for which BLOCK returns true.

    map() is a mapping function: send a value from LIST into BLOCK, and BLOCK returns a list of 0 or more values; the combined set of all of those calls to BLOCK will be the ultimate list returned by map().

提交回复
热议问题