设为首页 收藏本站
查看: 925|回复: 0

[经验分享] perl的array和map/hash

[复制链接]

尚未签到

发表于 2015-12-26 14:55:51 | 显示全部楼层 |阅读模式
  
  一 array
  1)实例



use strict;
use warnings;
my @myarray = (123,"hello", 456, 'guy');
foreach(@myarray)
{
    print "$_ " ;
}
print "\n";
foreach my $item (@myarray)
{
    print "$item " ;
}
print "\n";
for(my $i = 0; $i <scalar(@myarray); $i++)
{
    print "$myarray[$i]" . " ";
}
print "\n";
for(0..($#myarray))
{
    print "$myarray[$_]" . " ";
}
print "\n";
@myarray = (@myarray, 789);
print "@myarray\n" ;
push(@myarray,"gril");
print "@myarray\n" ;
unshift(@myarray, '000');
print "@myarray\n" ;
delete $myarray[1];
print "@myarray\n" ;  
  2)函数,如下:
DSC0000.png
  3)注释:
  1】使用@定义array,使用array的item时$array[n];
  2】使用scalar来获得array的size;
  3】$#获得最大的index,即size-1;
  4】$_在for和foreach中表示当前item;
  5】push/pop用来在array的最后加入和弹出item;
  6】shift/unshift用来在array的前面删除和插入item;
  7】split/john用来实现array和string间的转化;
  8】delete可以用来删除item,例如delete $myarray[1];
  
  二 map/hash
  1) 实例:



use strict;
use warnings;
my %myhash = ('k1',100,'k2',200);
# my %myhash = (k1=>100,k2=>200);
print %myhash ;
print "\n";
$myhash{'k3'} = 300;
$myhash{'k4'} = 400;
print %myhash ;
print "\n";
foreach my $key (keys %myhash)
{
    print $key . " indexes ".$myhash{$key}."\n";
}
foreach my $value (values %myhash)
{
    print $value ."\n";
}
while((my $key, my $value)= each(%myhash))
{
    print "$key indexes $value \n"
}
if(exists $myhash{'k1'}) {print "k1 is exist\n";}
delete $myhash{'k1'};
print %myhash ;
print "\n" ;

  
  2)函数,如下:
DSC0001.png
  3)注释:
  1】%用来定义map/hash;
2】对单个的key赋值是使用$,例如$myhash{'k3'} = 300;
3】keys获得所有的keys到array;
4】values获得所有的values到array;
5】迭代,每次返回一对key/value;
6】exists用来判断某个key是否存在;
7】delete用来删除指定的key,同时对应的value也被删除;
  
  三 总

Array Functions
@array = ( ); Defines an empty array
@array = (&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;); Defines an array with values
$array[0] The first element of @array
$array[0] = a; Sets the first element of @array to a
@array[3..5] Array slice - returns a list containing the 3rd thru 5th
elements of @array
scalar(@array) Returns the number of elements in @array
$#array The index of the last element in @array
grep(/pattern/, @array) Returns a list of the items in @array that matched
/pattern/
join(expr, @array) Joins @array into a single string separated by expr
push(@array, $var) Adds $var to @array
pop(@array) Removes last element of @array and returns it
reverse(@array) Returns @array in reverse order
shift(@array) Removes first element of @array and returns it
sort(@array) Returns alphabetically sorted @array
sort({$a<=>$b}, @array) Returns numerically sorted @array  


Hash Functions
%hash = ( ); Defines an empty hash
%hash = (a => 1, b=>2); Defines a hash with values
$hash{$key} The value referred to by this $key
$hash{$key} = $value; Sets the value referred to by $key
exists $hash{$key} True if the key/value pair exists
delete $hash{$key} Deletes the key/value pair specified by $key
keys %hash Returns a list of the hash keys  values %hash Returns a list of the hash values
  
  完!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156645-1-1.html 上篇帖子: 使用Perl操作DBM数据库 下篇帖子: perl命令行的使用(转载)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表