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

perl基础:perl中@_,和1,$2,...及其类似变量的含义

发布时间:2020-12-15 21:02:43 所属栏目:大数据 来源:网络整理
导读:Perl's a great language for special variables - variables that are set up without the programmer having to intervene and providing information ranging from the number of lines read from the current input file ( . ) t h r o u g h t h e c u

Perl's a great language for special variables - variables that are set up without the programmer having to intervene and providing information ranging from the number of lines read from the current input file ( .)throughthecurrentprocessID( )andtheoperatingsystem( ^O). Other special variables effect how certain operations are performed ( |controllingoutputbuffering/flushing,forexample),orarefundamentalintheoperationofcertainfacilities?nomoresothan _ and @_.

-------------------------------------------

1.@_含义

1)是perl中默认的数组变量

比如说你想移除数组中的一个元素赋值给一个变 value1@abcd??????my value=shift @abcd;

方法2:你没有定义任何数组
?????? my $value=shift @_;?? 和上例等效
?????? 这里perl会隐式的选择@_

2)是sub子函数中的默认参数列表.

例如:
sub funct($$) {
??? ($param1,$param2) = @_;
??? #Statement
}

再例如,有下面一段代码:
my?max_number?=?&max(1,2);?print?"1?and?2?,the?max?number?ismax_numbern";
sub max{
???? my ( num1, num2) = @_[0,1]; ## 取出参数列表中的元素。
???? ........此处省略求max运算
}

在子函数中直接shift; 就可以从@_的前端弹出一个元素.

shift;
等于
shift @_;

-------------------------

@_ is the list of incoming parameters to a sub.?So if you write a sub,you refer to the first parameter in it as?$_[0],the second parameter as?$_[1]?and so on. And you can refer to?$#_?as the index number of the last parameter:
sub demo {
??print "Called with ",#_+1,"?paramsn";???print?"First?param?was_[0]n";

Note that the?English?module adds in the ability to refer to the special variables by other longer,but easier to remember,names such as @ARG for @_ and? PIDfor $. But use English; can have a detrimental performance effect if you're matching regular expressions against long incoming strings

2.$_含义

1) print;print _;
2)默认模式匹配空间(pattern matching space)
s/.../.../;
等于
$_ =~ s/.../.../;

---------------------------

Then any regular expression matches,?chops (and?lcs and many more) without a parameter,and even?prints assume you want to work on? .Thus:while( line = <FH>) {
??if ( line=?/Perl/)????printFHO$line;??????printuc line;
??}

Shortens to:
while (<FH>) {
??/Perl/ and
????print FHO ;
??print uc;
??}
3. 1, 2,...等含义

以数字为名的变量保存的是上一次匹配操作(/pattern/)中,第n个小括号中的原符号所匹配内容。 ??
1 2就是第二对小括号中的原符号所对应的匹配内容。

内插功能: ?

str?=?"aaa4zzz7bbb"; str ? =~ /(d)z{3}(d)/; ??
print ? " 1t 2n"; ?? 输出结果是:4 ? ? 7 ?

(编辑:李大同)

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

    推荐文章
      热点阅读