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

做网站最快的编程语言网站建设方案优化

做网站最快的编程语言,网站建设方案优化,网站建设需要用到哪些软件有哪些,网线水晶头接法图解引言 记录自己跳进的【perl】编程小坑,以己为鉴。 1、eq $str1 "12345\n"; $str2 "12345"; if ($str1 eq $str2) { print "OK" } 上述代码不会打印 OK。特别在读文件 ,匹配字符串时容易出BUG。 案例说明: 有…



引言

记录自己跳进的【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.tj-hxxt.cn/news/90583.html

相关文章:

  • 网站引流怎么做如何做广告宣传与推广
  • 外贸电子商务网站建设找竞价托管公司
  • 网站建设 问卷调查长沙网站制作主要公司
  • 求做政府采购网站云南疫情最新情况
  • 什么网站做美式软装设计理念cps游戏推广平台
  • 郑州哪家公司做网站好广东深圳龙华区
  • wordpress建立seo的内容有哪些
  • 中牟网站建设企业网站seo平台
  • 网站备案取名网页制作
  • 安丘市住房与城市建设路网站企业网站制作与维护
  • 免费建立自己的网站网站推广服务报价表
  • wordpress获取用户的评论北京seo优化wyhseo
  • wordpress subway恩城seo的网站
  • 专业的营销型网站制作西安seo关键词排名优化
  • 黔东南网站建设gzklyy百度推广公司
  • 网站设计提案爱站长工具综合查询
  • 深圳品牌策划设计百度推广优化
  • 上海广告公司联系方式武汉seo搜索引擎
  • 服务器安全防护措施移动端优化
  • 北京网站开发最专业的公司2345中国最好的网址站
  • 莱芜举报网站建立网站要多少钱一年
  • 济南疫情防控最新通知seo手机端排名软件
  • 网站做的好有什么用网络营销推广系统
  • 网站长尾词排名做不上去衡水网站seo
  • 深圳的网站建设公司电话好的建站网站
  • 网购网站模板seo排名
  • 找兼职工作在家做正规网站长沙网站优化seo
  • 徐州好点的做网站的公司有哪些西安百度公司地址介绍
  • b2c电子商务模式的网站有哪些百度seo 站长工具
  • 网站上传文件惠州seo排名公司