open test_A_handle , "<D:\\Perl_WorkSpace\\test_A.txt\n" or die "can't open the file test_A.txt\n";
open test_B_handle , "<D:\\Perl_WorkSpace\\test_B.txt\n" or die "can't open the file test_B.txt\n";
$pos = tell(test_B_handle);#获取文件指针位置,因为刚打开文件,所以 $pos为0
while (my $str1 = <test_A_handle>)
{
seek(test_B_handle,$pos,0);#使文件指针回到文件头
my $cnt = 0;
while(my $str2 = <test_B_handle>)
{
$cnt++;
if ($str1 eq $str2)
{
print "match string :" . $str1 . " ";
print "line num : " . $cnt . "\n";
}
}
}
close test_A_handle;
close test_B_handle;
那么你得到的结果是:
perl复制代码
match string :1111111
line num : 5
match string :1111111
line num : 9
match string :2222222
line num : 6
match string :3333333
line num : 7
match string :4444444
line num : 8
match string :1234567
line num : 1
match string :0000000
line num : 2
match string :0978157
line num : 3
open test_A_handle , "<D:\\Perl_WorkSpace\\test_A.txt\n" or die "can't open the file test_A.txt\n";
open test_B_handle , "<D:\\Perl_WorkSpace\\test_B.txt\n" or die "can't open the file test_B.txt\n";
$pos = tell(test_B_handle);#获取文件指针位置,因为刚打开文件,所以 $pos为0
while (my $str1 = <test_A_handle>)
{
chomp $str1;
seek(test_B_handle,$pos,0);#使文件指针回到文件头
my $cnt = 0;
while(my $str2 = <test_B_handle>)
{
chomp $str2;
$cnt++;
if ($str1 eq $str2)
{
print "match string :" . $str1 . " ";
print "line num : " . $cnt . "\n";
}
}
}
close test_A_handle;
close test_B_handle;
输出:
2、split
perl复制代码
my $str_1 = "ab cd ef gh 12 34 56\n";
my @array_1 = split(' ',$str_1);
print @array_1;
print "1234567890";