温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Perl标量和数组的概念是什么

发布时间:2021-07-21 23:34:55 来源:亿速云 阅读:143 作者:chen 栏目:开发技术

本篇内容介绍了“Perl标量和数组的概念是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、Perl标量

1、Perl有三种变量:Perl标量、数组、哈希
2、字符相加不是用“+”号,而是用“.”
3、

#!/usr/bin/Perl-w  $num1="a";####“”是字符  $num2="5";  $num3=$num1x$num2;####$num1重复$num2次  print"\$num3is$num3\n";

执行结果:
[root@test-linuxtmp]#./pe.pl
$num3isaaaaa
[root@test-linuxtmp]#
4、Perl中字符串的比较操作和Shell中的数值测试运算相同,Perl中的数值比较操作就和Shell的字符串比较操作相同。
awk中“=”表示赋值“==”表示等于关系的判断

二、Perl数组--数组是由一组连续的Perl标量

1、Perl中使用@加上数组名来表示一个数组;Perl中的数组下标是从“0”开始;Perl中的数组元素不必是同一数据类型

2、使用[]申请数组中第几个元素

3、$name@name%name分别表示Perl标量数组散列

4、push能够把一些元素添加到数组尾部,而pop函数每次只能取走一个元素(是取走而不是复制)末理解的“堆栈数据结构”

[root@test-linuxtmp]#catpg.pl  #!/usr/bin/Perl-w  @list1=(1..4);  @list2=("zero","one","two","three","four");  push(@list1,@list2);  $last=pop(@list1);  print"\@list1is@list1\n";  print"\@list2is@list2\n";  print"\$lastis$last\n";  [root@test-linuxtmp]#vipg.pl  [root@test-linuxtmp]#./pg.pl  @list1is1234zeroonetwothree  @list2iszeroonetwothreefour  $lastisfour  [root@test-linuxtmp]#

5、unshift函数是在数组的头部插入一个或者是一些新的元素;shift是从数组的头部移走一个元素,整个数组看起来像是向左移动了一个位置。

#!/usr/bin/Perl-w  @list1=(0..4);  @list2=("zero","one","two","three","four");  unshift(@list1,@list2);###是将数组list2插入到数组list1  $last=shift(@list1);  print"\@list1is@list1\n\@list2is@list2\n\$lastis$last\n";   [root@test-linuxtmp]#./ph.pl  @list1isonetwothreefour01234  @list2iszeroonetwothreefour  $lastiszero  [root@test-linuxtmp]#

6、reverse函数的功能是颠倒数组,它可以把数组元素的顺序头尾颠倒。

[root@test-linuxtmp]#catpi.pl  #!/usr/bin/Perl-w  @list1=(0..4);  print"\@list1is@list1\n";  @list1=reverse(@list1);  print"\@list1reverseis@list1\n";   [root@test-linuxtmp]#./pi.pl  @list1is01234  @list1reverseis43210  [root@test-linuxtmp]#

7、哈希

哈希变量和数组非常类似,都可以存放多个Perl标量,每个Perl标量可以通过索引单独存取。不同的是哈希变量的索引不是数组的下标,而是另一个Perl标量。通常这个Perl标量被称作key,通过key,我们就可以访问
到其对应的数据。另一点的不同是哈希变量中的元素没有先后之分,是无序的,key是能够访问它们的惟一通道。Perl中使用百分号“%”来表示一个哈希变量。

为以下内容

[root@test-linuxtmp]#catpl.pl  #!/usr/bin/Perl-w  $area{'beijing'}=9;  $area{'shanghai'}=8;  print"$area{'beijing'}\n"  %areab=('hebei'=>5,'handan'=>4);  print"\$areabhebeiis$areab{'hebei'}\n";  print"\$areabhandanis$areab{'handan'}\n"  [root@test-linuxtmp]#

执行错误

[root@test-linuxtmp]#./pl.pl  Unquotedstring"areab"mayclashwithfuturereservedwordat./pl.plline5.  Operatororsemicolonmissingbefore%areabat./pl.plline5.  Ambiguoususeof%resolvedasoperator%at./pl.plline5.  Can'tmodifymodulus(%)inscalarassignmentat./pl.plline5,near");"  Executionof./pl.plabortedduetocompilationerrors.

但将2到4行注释掉可以成功执行。原因不清楚

[root@test-linuxtmp]#catpt.pl
#!/usr/bin/Perl-w
%area=(1,"a",2,"b");
print"\%areais1$area{1}\n";

“Perl标量和数组的概念是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI