打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Perl练习题,持续更新中...

自己做的,有更好的答案可以贴上来,电脑是英文系统,编辑器中文显示有问题,所以写的英文注释(英语小白,勿喷)

1.在目录/tmp下找到100个以abc开头的文件,然后把这些文件的第一行保存到文件new中。

  1. #user/bin/env perl  
  2.   
  3. #file name : test.pl  
  4. #author    : Tony Sun  
  5. #version   : 1.0  
  6. #date      : 2014-6-26 11:01:29  
  7.   
  8. =head function descripte  
  9.  read first line in each files which name start with $startwith in folder $path  
  10.  write it to a file named $write_file_name  
  11.    
  12.  $path           : the path of files  
  13.  $write_file_name: the file name which been wroted  
  14.  $startwith      : the ragular of the file name  
  15.  $num            : the number of the documents to satisfy the conditions  
  16. =cut  
  17.   
  18. sub func_find_and_save{  
  19.     my($path,$write_file_name,$startwith,$num) = @_;  
  20.       
  21.     #get folder&file handler  
  22.     opendir(TMP,$path) || die "open folder failed,cause by:$!";  
  23.     open(WRITEFILE,">$write_file_name") || die "open file $write_file_name failed,cause by:$!";  
  24.   
  25.     my $flag = 0;  
  26.     my $open_failed = 0;  
  27.       
  28.     #filte files and get the first line in the file which satisfy the condition  
  29.     foreach $file_name (readdir TMP)  
  30.     {  
  31.         next if(!($file_name =~ /^$startwith/));  
  32.           
  33.         $file = $path."/".$file_name;  
  34.         if(-f $file)  
  35.         {  
  36.             if(open(FILE,$file))  
  37.             {  
  38.                 $line = <FILE>;  
  39.                 print WRITEFILE $line;  
  40.                 $flag++;  
  41.                 close(FILE);  
  42.                 last if($flag>=$num);  
  43.             }  
  44.             else  
  45.             {  
  46.                 $open_failed++;  
  47.             }  
  48.         }  
  49.     }  
  50.   
  51.     #close handlers  
  52.     close(WRITEFILE);  
  53.     close(TMP);  
  54.       
  55.     #info user the result after execute this function  
  56.     if($flag==$num)  
  57.     {  
  58.         print "finished!\n";  
  59.     }  
  60.     else  
  61.     {  
  62.           
  63.         printf "got %d files which name start with \'$startwith!\'",$flag+$open_failed;  
  64.         print "open failed : $open_failed\n" if ($open_failed>0);  
  65.     }  
  66. }  
  67.   
  68. # function test  
  69. func_find_and_save("tmp","new","Action",100);  

2.写脚本实现,可以用shell、perl等。把文件b中有的,但是文件a中没有的所有行,保存为文件c,并统计c的行数。(忽然发现文件句柄都没关闭)

  1. #!user/bin/env perl  
  2.   
  3. #file name : test.pl    
  4. #author    : Tony Sun    
  5. #version   : 1.0    
  6. #date      : 2014-6-26 11:43:50  
  7.   
  8. =head function descripte  
  9.     two files named $file_a and $file_b,fetch rows that in $file_a and not in $file_b,  
  10.     write those rows to $file_c,and give out the row numbers in $file_c  
  11.       
  12.     $file_a : file for compare  
  13.     $file_b : file for compare  
  14.     $file_c : file for deposit row than in $file_a and not in $file_b  
  15. =cut  
  16.   
  17. sub func_compare_files{  
  18.     my($file_a,$file_b,$file_c) = @_;  
  19.       
  20.     #get file operate handler  
  21.     open("FILE_A","<$file_a") || die "open file $file_a failed,cause by:$!";  
  22.     open("FILE_B","<$file_b") || die "open file $file_b failed,cause by:$!";  
  23.     open("FILE_C",">$file_c") || die "open file $file_c failed,cause by:$!";  
  24.       
  25.     #get one row each time in $file_a,and compare with each rows in $file_b  
  26.     #if it is not in file_b,then print it out to file_c  
  27.     #(it maybe not the best method to handle this question,but it is the method think of now)  
  28.     my $diff_rows = 0;  
  29.     my @lines_b = <FILE_B>;  
  30.     while(my $line_a = <FILE_A>)  
  31.     {  
  32.         my $flag = 0;  
  33.         foreach my $line_b (@lines_b)  
  34.         {  
  35.             if($line_a eq $line_b)  
  36.             {  
  37.                 $flag++;  
  38.                 last;  
  39.             }  
  40.         }  
  41.         print FILE_C $line_a if($flag==0);  
  42.         $diff_rows++ if($flag==0);  
  43.     }  
  44.       
  45.     close(FILE_A);  
  46.     close(FILE_B);  
  47.     close(FILE_C);  
  48.       
  49.     print "finished,and $diff_rows rows in $file_c\n";  
  50. }  
  51.   
  52. #function test  
  53. func_compare_files("a","b","c");  

3.输入一个数字组成的字符串,插入“+”,“-”,使得计算结果为0.(非递归版和递归版)。正在做ing

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
perl 文件读写详细说明 - 开源中国社区
Nginx编译安装时常见错误分析
写入多个文件perl脚本
MySQL HandlerSocket in Action
在EXCEL中 如何用VBA查找某特定单元格并返回该单元格的行和列值?
Perl的简单语法(与C语言语法的异同)3
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服