perl 数组grep_Perl数组Grep()函数

perl 数组grep

The Perl grep() function is a filter that runs a regular expression on each element of an array and returns only the elements that evaluate as true. Using regular expressions can be extremely powerful and complex. The grep() functions uses the syntax @List = grep(Expression, @array).

Perl grep()函数是一个过滤器,它在数组的每个元素上运行正则表达式,并且仅返回评估为true的元素。 使用正则表达式可能非常强大和复杂。 grep()函数使用语法@List = grep(Expression,@array)。

使用Grep()函数返回真表达式 ( Using Grep() Function to Return True Expressions )


@myNames = ('Jacob', 'Michael', 'Joshua', 'Mathew', 'Alexander', &


@grepNames = grep(/^A

Think of the @myNames array as a row of numbered boxes, going from left to right and numbered starting with a zero. The grep() function goes through each of the elements (boxes) in the array and compares their contents to the regular expression. If the result is true, the contents are then added to the new @grepNames array.

将@myNames数组视为一排带编号的框,从左到右并从零开始编号。 grep()函数遍历数组中的每个元素(框),并将其内容与正则表达式进行比较。 如果结果为true ,则将内容添加到新的@grepNames数组中。

In the above example, the regular expression /^A/ is looking for any value that starts with a capital A. After sifting through the contents of the @myNames array, the value of @grepNames becomes ('Alexander', 'Andrew'), the only two elements that start with a capital A.

在上面的示例中,正则表达式/ ^ A /正在查找以大写字母A开头的任何值。在@myNames数组的内容中进行筛选后,@ grepNames的值变为('Alexander','Andrew') ,只有两个以大写字母A开头的元素。

反转Grep()函数中的表达式 ( Reversing the Expression in a Grep() Function )

One quick way to make this particular function more powerful is to reverse the regular expression with the NOT operator. The regular expression then looks for elements that evaluate to false and moves them into the new array.

使此特定功能更强大的一种快速方法是使用NOT运算符反转正则表达式。 然后,正则表达式将查找结果为false的元素,并将其移动到新数组中。


@myNames = ('Jacob', 'Michael', 'Joshua', 'Mathew', 'Alexander', &


@grepNames = grep(!/^A

In the above example, the regular expression is looking for any value that does not start with a capital A. After sifting through the contents of the @myNames array, the value of @grepNames becomes ('Jacob', 'Michael', 'Joshua', 'Matthew').

在上面的示例中,正则表达式正在寻找不以大写字母A开头的任何值。在@myNames数组的内容中进行筛选后,@ grepNames的值变为('Jacob','Michael','Joshua ', '马修')。

关于Perl ( About Perl )

Perl is an adaptable programming language frequently used to develop web applications. Perl is an interpreted, not compiled, language, so its programs take up more CPU time than a compiled language—a problem that becomes less important as the speed of processors increases. However, writing in Perl is faster than writing in a compiled language, so the time you save is yours. 

Perl是一种经常用于开发Web应用程序的适应性编程语言。 Perl是一种解释性而非编译语言,因此它的程序比编译语言占用更多的CPU时间-随着处理器速度的提高,此问题变得不那么重要了。 但是,用Perl编写要比用编译语言编写要快,因此节省的时间就是您自己的。

翻译自: https://www.thoughtco.com/perl-array-grep-function-quick-tutorial-2641158

perl 数组grep

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
perl函数手册英文版 Perl提供了可以直接调用的、数目众多的函数。可以对以下对象进行操作: 数组:chomp, join, keys, map, pop, push, reverse, shift, sort, splice, split, unshift, values 数据库:dbmclose, dbmopen 目录:chdir, closedir, mkdir, opendir, readdir, rewinddir, rmdir, seekdir, telldir 文件:binmode, chdir, chmod, chown, chroot, close, eof, fnctl, fileno, flock, getc, glob, ioctl, link, lstat, open, print, printf, read, readdir, readlink, rename, rmdir, seek, select, stat, symlink, sysopen, sysread, syswrite, tell, truncate, umask, unlink, utime, write 组:endgrent, getgrent, getgrgid, getgrname, getpgrp, setgrent, setpgrp Hash: delete, each, exists, keys, values 主机:endhostent, gethostbyaddr, gethostbyname, sethostent 输入:getc, read, sysread 处理器间通讯:msgctl, msgget, msgrcv, msgsnd, pipe, semctl, semget, semop, shmctl, shmget, hmread, shmwrite 数学:abs, atan2, cos, exp, hex, int, log, oct, rand, sin, sqrt, srand 消息队列:msgctl, msgget, msgrcv, msgsnd 其他:bless, defined, do, eval, formline, import, ref, scalar, syscall, tie, tied, undef, untie, wantarray 网络:endnetent, getnetbyaddr, getnetbyname, getnetent, setnetent 输出:die, print, printf, syswrite, warn, write 口令:endpwent, getpwent, getpwname, getpwuid, setpwent 进程:alarm, die, dump, exec, exit, fork, getlogin, getpgrp, getppid, getpriority, kill, setpriority, sleep, system, times, umask, wait, waitpid 协议:endprotent, getprotobyname, getprotobynumber, getprotoent, getservbyname, getservbyport, getservent, setprotoent 正则表达式grep, pos, quotemeta, reset, split, study 范围:caller, local, my 服务:endservent, getservbyname, getservbyport, getservent, setservent 套节字:accept, bind, connect, gethostbyaddr, gethostbyname, gethostent, getpeername, getservbyname, getservbyport, getservent, getsockname, getsockopt, listen, recv, select, send, setsockopt, shutdown, socket, socketpair 字符串:chop, chr, crypt, hex, index, join, lc, lcfirst, length, oct, Ord, pack, q, qq, quotemeta, qw, qx, reverse, rindex, split, sprintf, substr, uc, ucfirst, unpack, vec 时间:gmtime, localtime, time UNIX: chmod, chown, chroot, dump, endgrent, endhostent, endnetent, endprotent, endpwent, endservent, fnctl, fork, getgrent, getgrgid, getgrname, gethostent, getlogin, getnetent, getpgrp, getppid, getpriority, getprotobyname, getprotobynumber, getprotoent, getpwent, getpwname, getpwuid, getservbyname, getservbyport, getservent, ioctl, link, lstat, readlink, select, setgrent, sethostent, setnetent, setpgrp, setpriority, setprotoent, setpwent, setservent, sleep, syscall, times, umask, wait, waitpid

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值