加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl两个神奇的数组@-和@+

发布时间:2020-12-16 00:21:54 所属栏目:大数据 来源:网络整理
导读:[root@stationx ch2-regular]# cat perl-grep4.pl #!/bin/perl # perl-grep4.pl my $pattern = shift @ARGV; my $regex = eval { qr/$pattern/ }; die "Check your pattern! $@" if $@; while() ??????? { ??????? if(m/$regex/) ??????????????? { ????????

[root@stationx ch2-regular]# cat perl-grep4.pl
#!/bin/perl
# perl-grep4.pl
my $pattern = shift @ARGV;
my $regex = eval { qr/$pattern/ };
die "Check your pattern! $@" if $@;
while(<>)
??????? {
??????? if(m/$regex/)
??????????????? {
??????????????? print "$_";
??????????????? print "tt$&: ",
??????????????? substr( $_,$-[$i],$+[$i] - $-[$i] ),
??????????????? "n";
??????????????? foreach my $i ( 1 .. $#- )
??????????????????????? {
??????????????????????? print "$#-n";
??????????????????????? print "tt$$i: ",
??????????????????????? substr( $_,
??????????????????????? "n";
??????????????????????? }
??????????????? }
??????? }

[root@stationx ch2-regular]# perldoc -t perl| perl perl-grep4.pl "b(S)(S)1b"

?http://www.cpan.org/?????? the Comprehensive Perl Archive
??????????????? $&: www
??????????????? $1: w
??????????????? $2: w
???? http://www.pm.org/???????? the Perl Mongers ??????????????? $&: www ??????????????? $1: w ??????????????? $2: w [root@stationx ch2-regular]# @-和@+两个数组非常的神奇,两者保存的是matched内容的offset(偏移量)????????? 也就是说每个match内容的起始offset值放置在@-数组中,结束offset值放置在@+数组中????????? 说白了@-和@+两个数组保存的是一系列的值。????????? @-数组的第一个元素是matche内容的起始offset,@+数组的第一个元素是match内容的结尾offset????????? 随后才是$1,$2...的offset值。?????????

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读