在Perl中grep和map之间有什么区别?

编程入门 行业动态 更新时间:2024-10-16 02:25:32
本文介绍了在Perl中grep和map之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Perl中, grep 和 map 采用表达式和列表,并评估每个元素的表达式列表。

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

两者之间有什么区别?

推荐答案

grep 返回与表达式匹配的原始列表中的元素,而 map 返回表达式的结果应用于原始列表的每个元素。

grep returns those elements of the original list that match the expression, while map returns the result of the expression applied to each element of the original list.

$ perl -le 'print join " ", grep $_ & 1, (1, 2, 3, 4, 5)' 1 3 5 $ perl -le 'print join " ", map $_ & 1, (1, 2, 3, 4, 5)' 1 0 1 0 1

第一个例子打印列表的所有奇数元素,而第二个示例打印一个0或1,取决于相应元素是否为奇数。

The first example prints all the odd elements of the list, while the second example prints a 0 or 1 depending on whether the corresponding element is odd or not.

更多推荐

在Perl中grep和map之间有什么区别?

本文发布于:2023-11-25 07:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   Perl   grep   map

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!