Some perl tips
下面是日常工作中一些代码片段的总结,部分注释是后加的,采用了//这种形式,请勿套用。
1.取得用户输入
print("Please input the date range:");
$dateRange=<STDIN>;
chomp($dateRange);
2.如果数据不符合要求退出程序
if(!isValidDateRange($dateRange)){
die("Wrong date range\n");
}
3.劈分字符串得到数组
my @arrDate=split(/\s*[~,-]\s*/,$dateRange);
$startDate=@arrDate[0];
$endDate=@arrDate[-1]; // 倒数第一个,perl数组的这个特性真是太贴心了,比C一脉的长度减一要省事不少,不过也有elsif这种你奈我何的任性设计
4.在屏幕上打印文本
print("Reading file...\n");
5.逐行读取文件
my @arrFileFound;
open(FOF, $fof) || die "Could not read $fof:$!";
while ($line = <FOF>){
# read a line from file
chomp($line);
# fill the arrFileFound with line
push(@arrFileFound,$line); // 读出的行放入数组
}
close(FOF);
6.数组排序
@arrFileFound=sort(@arrFileFound);
7.遍历数组
# 初始化数组
@annoNames=("danile","iria","jinwn","ka","manzhez","marna","max_nglish","mohmed","roxna","thir","trno","trno_english","transtc_en");
$nNames=@annoNames;// 取数组长度
$iNames=0; // 遍历下标
while($iNames<$nNames){
$annoName=$annoNames[$iNames];
# do sth
...
$iNames++;
}
8.将数组作为参数传入函数
函数定义:
sub printAnnologs{
local($annotator,*files)=@_;
$n=@files;
$i=0;
while($i<$n){
my $file=@files[$i];
...
$i++;
}
}
调用方式
&printAnnologs($annoName,*annologs);
9.判断某字符串是否包含另一字符串
sub isValidAnnotator{
local($name)=@_;
my $names="danile","iria","jinwn","ka","manzhez","marna","max_nglish","mohmed","roxna","thir","trno","trno_entac_en";
return index($names,$name)!=-1;
}
10.正则表达式模式匹配
sub isValidDateRange{
local($date)=@_;
return $date=~/^\d{8}\s*[-~,]\s*\d{8}$/;
}
11.字符串替换
#将$newfile中的USA替换成America
my $newfile="ChinaBeijingUSANewyork";
$newfile=~s/USA/America/;
相关文章:http://www.cnblogs.com/xiandedanteng/p/3250688.html
Some perl tips的更多相关文章
- 【python】列出http://www.cnblogs.com/xiandedanteng中所有博文的标题
代码: # 列出http://www.cnblogs.com/xiandedanteng中所有博文的标题 from bs4 import BeautifulSoup import requests u ...
- Node.js 网页爬虫再进阶,cheerio助力
任务还是读取博文标题. 读取app2.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块, ...
- Node.js 网页瘸腿稍强点爬虫再体验
这回爬虫走得好点了,每次正常读取文章数目总是一样的,但是有程序僵住了情况,不知什么原因. 代码如下: // 内置http模块,提供了http服务器和客户端功能 var http=require(&qu ...
- Node.js 网页瘸腿爬虫初体验
延续上一篇,想把自己博客的文档标题利用Node.js的request全提取出来,于是有了下面的初哥爬虫,水平有限,这只爬虫目前还有点瘸腿,请看官你指正了. // 内置http模块,提供了http服务器 ...
- perl的几个小tips
1.字符串比较 if($str eq "hello"){ ... } 字符串比较必须用eq,用==只适合标量 2.字符串连接 $str3=$str1."-".$ ...
- SUSE linux升级perl及openssl
一.perl安装: 1.下载并解压软件:tar zxvf perl-5.24.0.tar.gz 2.运行./configure.gnu -help查看帮助,运行./configure.gnu -des ...
- perl代码调试
perl调试教程 一.DESCRIPTIONA (very) lightweight introduction in the use of the perl debugger, and a point ...
- iOS/Swift Tips 1
1.重写hitTest方法,干预iOS事件传递过程 如下所示,view上有一个button,button一半的frame在父类view bounds之外, 按照iOS系统默认的处理逻辑, 如果点击按钮 ...
- perl 对ENV环境变量的使用
1.hash 方式访问. %ENV key为环境变量名,value为环境变量值 2.调用ENV模块 . use Env qw(PATH); print "path is $ENV{path ...
随机推荐
- POJ -1679(次小生成树)模板
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions:34617 Accepted: 12637 D ...
- sql 查找表引用的存储过程
USE [master] GO /****** Object: StoredProcedure [dbo].[uspGetDepends] Script Date: 05/12/2016 14:11: ...
- sql 查找死锁对象的存储过程
USE [master] GO /****** Object: StoredProcedure [dbo].[sp_who_lock] Script Date: 05/12/2016 14:13:46 ...
- 结构型设计模式之享元模式(Flyweight)
结构 意图 运用共享技术有效地支持大量细粒度的对象. 适用性 一个应用程序使用了大量的对象. 完全由于使用大量的对象,造成很大的存储开销. 对象的大多数状态都可变为外部状态. 如果删除对象的外部状态, ...
- suse10与suse11连接Xmanager的配置
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://feidandelion.blog.51cto.com/1050439/42217 ...
- 【linux高级程序设计】(第十一章)System V进程间通信 3
信号量通信机制 可以看到,跟消息队列类似,也是包括两个结构. int semget (key_t __key, int __nsems, int __semflg) : 创建信号量集合 第一个参数:f ...
- mysql 主从手动切换
将主从(3307主--3308从)切换 前提:3307正常 一.将3307设为只读.命令行操作 # 修改配置文件 vim /etc/mysql/mysql-//my.cnf # 在[mysqld]中增 ...
- poj 1389(离散化+计算几何)
Area of Simple Polygons Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3412 Accepted ...
- springboot微服务的简单小结
springboot微服务的简单小结 近来公司用springboot微服务,所以小结一下. 基础: 什么是SpingBoot微服务? 如何创建SpringBoot微服务? 如何管理和完善SpringB ...
- Maximum Product of Word Lengths -- LeetCode
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...