当前位置: 首页 > 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.hengruixuexiao.com/news/27971.html

相关文章:

  • 国际跨境电商平台有哪些九江seo优化
  • 一个公司网站设计需求营销文案
  • 电子商务网站规划方案新乡seo优化
  • 在家做网站编辑外贸找客户有什么网站
  • 做婚礼网站的公司简介交换链接平台
  • 山西太原建站怎么做搜索量排名
  • 公司网站哪里好关联词有哪些
  • 建设网站需申请什么资料山西百度推广开户
  • 甘肃省建设厅质量投诉网站网站页面优化方法
  • 手机wordpress建站教程网站推广包括
  • 怎么去推广一个网站百度旅游官网
  • 建设彩票网站需要哪些要求国外域名注册
  • 如何把资料上传到网站seo推广员是做什么的
  • wordpress评论去掉网址seo怎么发布外链
  • 日本网站制作需要多少钱网站推广找客户
  • wordpress商品采集器武汉seo网站排名优化
  • 网站建设java前后台好处天津百度seo排名优化
  • 机房建设网站模板百度一下你就知道手机版官网
  • 成都企业网站建设模板系统优化软件
  • WordPress建影视站简述企业网站推广的一般策略
  • 深圳市手机网站建设公司做企业推广
  • 花店商城网站设计seo入门培训教程
  • 南昌商城网站建设公司网站备案查询官网
  • 杭州经营性网站备案免费企业建站
  • 亳州是网站建设地推app推广赚佣金
  • 用网站做CAN总线通信好吗烟台seo网络推广
  • 卡片式网站咸阳seo
  • 网站需要每个城市做推广吗品牌营销策略研究
  • 做调查赚钱哪些网站最靠谱吗sem竞价广告
  • 自个做网站教程天津的网络优化公司排名