加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl练习题,持续更新中...

发布时间:2020-12-15 23:56:35 所属栏目:大数据 来源:网络整理
导读:?? 自己做的,有更好的答案可以贴上来,电脑是英文系统,编辑器中文显示有问题,所以写的英文注释(英语小白,勿喷) 1.在目录/tmp下找到100个以abc开头的文件,然后把这些文件的第一行保存到文件new中。 #user/bin/env perl#file name : test.pl#author : Ton
??

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

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

#user/bin/env perl

#file name : test.pl
#author    : Tony Sun
#version   : 1.0
#date      : 2014-6-26 11:01:29

=head function descripte
 read first line in each files which name start with $startwith in folder $path
 write it to a file named $write_file_name
 
 $path           : the path of files
 $write_file_name: the file name which been wroted
 $startwith      : the ragular of the file name
 $num            : the number of the documents to satisfy the conditions
=cut

sub func_find_and_save{
	my($path,$write_file_name,$startwith,$num) = @_;
	
	#get folder&file handler
	opendir(TMP,$path) || die "open folder failed,cause by:$!";
	open(WRITEFILE,">$write_file_name") || die "open file $write_file_name failed,cause by:$!";

	my $flag = 0;
	my $open_failed = 0;
	
	#filte files and get the first line in the file which satisfy the condition
	foreach $file_name (readdir TMP)
	{
		next if(!($file_name =~ /^$startwith/));
		
		$file = $path."/".$file_name;
		if(-f $file)
		{
			if(open(FILE,$file))
			{
				$line = <FILE>;
				print WRITEFILE $line;
				$flag++;
				close(FILE);
				last if($flag>=$num);
			}
			else
			{
				$open_failed++;
			}
		}
	}

	#close handlers
	close(WRITEFILE);
	close(TMP);
	
	#info user the result after execute this function
	if($flag==$num)
	{
		print "finished!n";
	}
	else
	{
		
		printf "got %d files which name start with '$startwith!'",$flag+$open_failed;
		print "open failed : $open_failedn" if ($open_failed>0);
	}
}

# function test
func_find_and_save("tmp","new","Action",100);

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

#!user/bin/env perl

#file name : test.pl  
#author    : Tony Sun  
#version   : 1.0  
#date      : 2014-6-26 11:43:50

=head function descripte
	two files named $file_a and $file_b,fetch rows that in $file_a and not in $file_b,write those rows to $file_c,and give out the row numbers in $file_c
	
	$file_a : file for compare
	$file_b : file for compare
	$file_c : file for deposit row than in $file_a and not in $file_b
=cut

sub func_compare_files{
	my($file_a,$file_b,$file_c) = @_;
	
	#get file operate handler
	open("FILE_A","<$file_a") || die "open file $file_a failed,cause by:$!";
	open("FILE_B","<$file_b") || die "open file $file_b failed,cause by:$!";
	open("FILE_C",">$file_c") || die "open file $file_c failed,cause by:$!";
	
	#get one row each time in $file_a,and compare with each rows in $file_b
	#if it is not in file_b,then print it out to file_c
	#(it maybe not the best method to handle this question,but it is the method think of now)
	my $diff_rows = 0;
	my @lines_b = <FILE_B>;
	while(my $line_a = <FILE_A>)
	{
		my $flag = 0;
		foreach my $line_b (@lines_b)
		{
			if($line_a eq $line_b)
			{
				$flag++;
				last;
			}
		}
		print FILE_C $line_a if($flag==0);
		$diff_rows++ if($flag==0);
	}
	
	close(FILE_A);
	close(FILE_B);
	close(FILE_C);
	
	print "finished,and $diff_rows rows in $file_cn";
}

#function test
func_compare_files("a","b","c");

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

??
??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读