当前位置: 首页 > news >正文

【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为0while (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 :1111111line num : 5
match string :1111111line num : 9
match string :2222222line num : 6
match string :3333333line num : 7
match string :4444444line num : 8
match string :1234567line num : 1
match string :0000000line num : 2
match string :0978157line 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为0while (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”

http://www.lryc.cn/news/384526.html

相关文章:

  • MIX OTP——使用 GenServer 进行客户端-服务器通信
  • 2024年云安全发展趋势预测
  • java.io.eofexception:ssl peer shut down incorrectly
  • Unity之HTC VIVE Cosmos环境安装(适合新手小白)(一)
  • 入门JavaWeb之 Response 验证码和重定向
  • 2024-06-26 问AI: 在大数据模型中,deep speed 是什么?
  • mobaxterm x11 转发Ubuntu mac
  • python数据分析实训任务三(‘职业’)
  • vscode连接SSH
  • 金融科技行业创新人才培养与引进的重要性及挑战
  • 【C++题解】1714. 输出满足条件的整数4
  • 如何安装和配置 Django 与 Postgres、Nginx 和 Gunicorn
  • Graphwalker基于模型的自动化测试
  • Macbook M1 Fusion安装Debian/Linux
  • ERP收费模式是怎样的?SAP ERP是如何收费的?
  • 如何实现免交互
  • 浏览器userAgent大全及JS判断当前APP
  • 11.异常(java版)
  • 单片机学习记录
  • flask的基本使用1
  • 如何编写时区源文件
  • 植物大战僵尸杂交版v2.1最新整合版,附PC端+安卓端+iOS端安装包+修改器+安装教程!
  • 【5G射频基本架构】
  • 4.任务调度
  • Github 2024-06-27 Go开源项目日报Top10
  • 【D3.js in Action 3 精译】1.2.2 可缩放矢量图形(一)
  • 「C系列」C 排序算法
  • Power BI可视化表格矩阵如何保持样式导出数据?
  • 《UDS协议从入门到精通》系列——图解0x35:请求上传
  • Tailwindcss 扩展默认配置来自定义颜色