免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 946 | 回复: 0

perl 常用函数-2 [复制链接]

论坛徽章:
0
发表于 2011-12-22 08:54 |显示全部楼层
1、hash中keys,value应用

  1. #!/usr/bin/perl -w
  2. use strict;

  3. my %hash = (
  4.         'add' => "guanghzou",
  5.         'tel' => "18602725010",
  6.         'job' => "Linux Administrator",
  7.         'name'=> "zhangtao",
  8.         'age' => "24",
  9.         'merry' => "no"
  10. );

  11. for (keys %hash) {
  12.         print "current key is $_, value $hash{$_}\n";
  13. }

  14. #使用map函数,打印时,格式美观
  15. print map { "current value : $_\n" } values %hash;
2、合并两个数组为一个hash

  1. #!/usr/bin/perl -w
  2. use strict;

  3. my %hash;
  4. my @ele1 = ('hostname','ip','os','https');
  5. my @ele2 = ('puppet.ctrl.cn','192.168.2.1','CentOS 5.5 x86_64','support');

  6. @hash{ @ele1 } = @ele2;     #注意这里hash以‘@’开头,第一个数据的元素为hash的key,第二个数组元素为value
  7. print " $_ $hash{$_}\n" for sort keys %hash;
  8. print map { "current key $_\n" } keys %hash;
3、将hash赋值给数组

  1. #将hash赋值给数组,新的数组以key,value,...形式组成
  2. my @keys = sort keys %hash;
  3. my @values = sort values %hash;
  4. print map { "$_\n" } @keys;
  5. print map { "$_\n" } @values;

  6. my @all = %hash;
  7. print map { "$_\n" } @all;

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP