perl比较两个文件字符串的实例代码
更新时间:2013年02月09日 23:25:16 作者:
perl比较两个文件字符串的例子,供大家学习参考
需求:取文件1中的一行,和文件2中所有的数据进行比较,有相同的保存起来,否则删除。
#!/usr/bin/perl
#use strict;
open(FILE1,"C:/Perl/BX/BX-Users.txt");
open(FILE2,"C:/Perl/BX/BX-Book-Ratings.txt");
open(result1,">C:/perl/BX/BX-Users_result.txt");
my $i=0;
my $j=0;
while((my $bxUser=<FILE1>)&&($i<10))
{
my $userId=substr($bxUser,0,index($bxUser,","));
while(my $rankUser=<FILE2>)
{
my $userIdCmp=substr($rankUser,0,index($rankUser,","));
if(($userId==$userIdCmp)&&($j==0))
{
syswrite(result1,"$bxUser");
$j++;
}
}
$j=0;
$i++;
}
close(FILE1);
close(FILE2);
close(result1);
复制代码 代码如下:
#!/usr/bin/perl
#use strict;
open(FILE1,"C:/Perl/BX/BX-Users.txt");
open(FILE2,"C:/Perl/BX/BX-Book-Ratings.txt");
open(result1,">C:/perl/BX/BX-Users_result.txt");
my $i=0;
my $j=0;
while((my $bxUser=<FILE1>)&&($i<10))
{
my $userId=substr($bxUser,0,index($bxUser,","));
while(my $rankUser=<FILE2>)
{
my $userIdCmp=substr($rankUser,0,index($rankUser,","));
if(($userId==$userIdCmp)&&($j==0))
{
syswrite(result1,"$bxUser");
$j++;
}
}
$j=0;
$i++;
}
close(FILE1);
close(FILE2);
close(result1);
相关文章
Perl 和 StrawberryPerl 与 ActivePerl 的区别详解
这篇文章主要介绍了Perl 和 StrawberryPerl 与 ActivePerl 的区别详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-12-12
Windows和Linux系统下perl连接SQL Server数据库的方法
这篇文章主要介绍了Windows和Linux系统下perl连接SQL Server数据库的方法,本文详细的讲解了Windows和Linux系统中perl如何连接Microsoft SQL Server数据库,需要的朋友可以参考下2014-10-10
Perl中使用dig和nali判断DNS解析地址归属地是否一致脚本分享
这篇文章主要介绍了Perl中使用dig和nali判断DNS解析地址归属地是否一致脚本分享,本文同时介绍了一个使用纯真IP库实现的Perl查询工具,需要的朋友可以参考下2014-09-09
Perl中的符号 ->;、=>; 和 :: 分别表示什么意思?
这篇文章主要介绍了Perl中的符号 ->;、=>; 和 :: 分别表示什么意思,需要的朋友可以参考下2017-10-10


最新评论