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 ...
随机推荐
- codeforces ~ 1004 C Sonya and Robots (dp)
C. Sonya and Robots time limit per test 1 second memory limit per test 256 megabytes input standard ...
- code forces 999C Alphabetic Removals
C. Alphabetic Removals time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- linux低权限执行高权限
1.关于sudo不需要输密码,低权限执行高权限,在root下的命令visudo放开%wheel ALL:保存退出, 执行gpasswd -a yourusername wheel 2.脚本命令下的,权 ...
- Bzoj3093 [Fdu校赛2012] A Famous Game
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 251 Solved: 136 Description Mr. B and Mr. M like to ...
- poj 3071 Football (概率DP水题)
G - Football Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- OpenGL函数思考-glColor
http://blog.csdn.net/shuaihj/article/details/7231980 OpenGL函数思考-glColor 函数原型: glColor3b,glColor ...
- 基于V4L2的视频驱动开发【转】
转自:http://blog.chinaunix.net/uid-10747583-id-298573.html Tags:V4L2驱动框架.API.操作流程…… 原文地址:http://www.ee ...
- java基础练习 17
import java.util.Scanner; public class Seventheen { /*企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元 ...
- 关于boostrapValidator动态添加字段(addField)验证的bug
每次码博客,都觉得自己怀才不遇,哎~脑袋有瑕疵,文笔拿不粗手,就直接上干货吧. 在使用boostrapValidator这个验证插件的时候,如果某一个字段是动态添加来的,我们需要调用方法:addFie ...
- CI中SESSION的用法及其注意
销毁 Session要清除当前 session: $this->session->sess_destroy(); 注意: 此函数应该是最后被调用的.即使闪出变量已不再有效.如果你只想让某几 ...