trzxycx 发表于 2017-5-17 08:58:14

#perl 重要练习

  数组,列表练习--小游戏的例子
























































































 
  
  文件练习 























































































  模式匹配练习-- 一道包括数组、文件和模式匹配的综合练习题:
  编写一个短程序,是他能够执行下列操作:
  1)打开一个文件
  2)将所有文件行读入一个数组
  3)从每行中取出所有单词
  4)找出至少拥有4个连续辅音或非原因字符的所有单词(比如"thoughts" 或者"yardstick")
  open(MFILE,"file.txt");
  @lines=<MFILE>;
  close(MFILE);
  foreach (@lines){
  if(m/[^aeiou]{4,}/i){
  print $_;
  }
  }
  哈希结构练习








































































  函数练习 












































































  亿阳经典脚本: 见附件
  -------------------------------------------------------------------------------------------------
  perl常用函数:
  chop
  chomp
  split(/pattern/,$text,limit)
  join($string,@array)
  index($string,$substring,position)
  substr($string,offset,length)
  grep(/pattern/,@array)
  push
  sort(@array)
  length($string)
  -------------------
  join函数:
  @array=("one","two","three");
  $total=join(":",@array); 这时$total="one:two:three";
  -------------------
  匹配运算符:m// 
  @F=$phrase=~m/\w/g;
for($i=0;$i<@F;$i++){
   print $F[$i].".";
}
  -------------------
  grep函数
  语法:grep(/pattern/,@array)
  说明:将合文字处理模式(regular expression)的数组元素找出来。
  示例:
  @array=("one","on","in");
  $count=grep(/on/,@array); #这时$count=2
  @result=grep(/on/,@array);#这时@result=("one","on");
  eg.1  @array=qw(grephound bloodhound terrier mutt chihuahua);
  @result=grep(s/hound/hounds/,@array);
  foreach $tmp (@result){
  print $tmp." ";
  }
  @result=grep(length($_)>5,@array);
  foreach $tmp (@result){
  print $tmp." ";
  }
  -------------------
  指令:push
  语法:push(@array,$string)
  说明:在数组@array的最后附加新的元素 ($string)到数组@array中。
  示例:@array=("one","two"); push(@array,"three"); #这时$@array=("one","two","three")
  -------------------
  hash相关:
  指令:keys
  语法:keys(%array)
  说明:取出关联数组%ARRAY中全部的key。
  示例:%NAME=(1,"mike",2,"michael"); @readkey=keys(%NAMES); #这时@readkey=(1,2);
  --------------------------------------------------------------------------------
  指令:values
  语法:values(%array)
  说明:取出关联数组%ARRAY中全部的value。
  示例:%NAMES=(1,"mike",2,"michael"); @readval=values(%NAMES);
  #这时@readval=("mike","michael");
  参考:http://www.freeoa.net/index.php/programs/21-perlpp/330-perl
页: [1]
查看完整版本: #perl 重要练习