【perl】脚本编程的一些坑&案例



引言

记录自己跳进的【perl】编程小坑,以己为鉴。


1、eq

$str1 = "12345\n"; $str2 = "12345";

if ($str1 eq $str2)

{

        print "OK"

}

上述代码不会打印 OK。特别在读文件 ,匹配字符串时容易出BUG。

案例说明:

有一个需求,对于test_A.txt文件的每一行,找出test_B.txt文件中与之相同的内容,打印该行内容,并显示行号。

test_A.txt 以及 test_B.txt 文件中内容:

如果你的代码是这么写的:

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;

那么你得到的结果是:

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

乍一看没啥毛病,但是细看发现test_A.txt文件中第一行,在test_B.txt文件的第12行也出现了,但是没有匹配到。原因在哪里呢?因为test_B.txt文件的第12行是最后一行,行末没有换行符\n,因为perl认为 "1111111" 不等于 "1111111\n"。那么我们在比较之前用chomp函数将换行符去掉即可解决这个小BUG。代码:

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 

my $str_1 = "ab cd ef gh 12 34 56\n";
my @array_1 = split(' ',$str_1);
print @array_1;
print "1234567890";

上述代码片段输出结果是什么?

abcdefgh1234561234567890

还是

abcdefgh123456

1234567890

split拆分之后,将\n去除了。也即是说$array_1[6] = "56" 而非 “56\n”

3、删除文件中特定的行

案例:删除文件中以 1 开头的行:

1、以读写方式打开文件

2、将文件指针置于文件头

3、按行取出文件内容存于数组

4、将文件大小截为0

5、去除数组中以 1 开头的元素

6、将修改后的文本写入文件中

7、关闭文件

#读写方式打开文件,默认文件指针位于文件末尾
open test_A_handle , "+>>D:\\Perl_WorkSpace\\test_A.txt\n" || die "can't open the file test_A.txt\n";
seek(test_A_handle,0,0);           #将文件指针放在文件首部
my @array_file_A = <test_A_handle>;#按行取出文件内容存于数组
my $truncate_flag = truncate(test_A_handle, 0);#将文件大小截为0
if($truncate_flag)
{
    print ">>The file has been truncated to 0.\n"
}
else
{
    print ">>Truncate Failled!\n"
}
my @array_file_A_NOT = grep{! /^1/} @array_file_A; #去除数组中以 1 开头的元素
print test_A_handle @array_file_A_NOT;             #将修改后的文本写入文件中
close test_A_handle;#关闭文件

写入前:

fq3fc3ibt
1fqokrgnb
2fqobfg
1fbrgqgnpq
cbqeovbier
333333333
21e413
f134t
rf1t
2fv2regh2qe
vgqweuivt
23526
fgquif
fgiuqfgq
23fiuf
2\n\t\vqaibr
fvqwieu
1rqgnrinqpg

写入后:

fq3fc3ibt
2fqobfg
cbqeovbier
333333333
21e413
f134t
rf1t
2fv2regh2qe
vgqweuivt
23526
fgquif
fgiuqfgq
23fiuf
2\n\t\vqaibr
fvqwieu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在路上-正出发

哈哈,多少是个心意

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值