下面是日常工作中一些代码片段的总结,部分注释是后加的,采用了//这种形式,请勿套用。

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的更多相关文章

  1. 【python】列出http://www.cnblogs.com/xiandedanteng中所有博文的标题

    代码: # 列出http://www.cnblogs.com/xiandedanteng中所有博文的标题 from bs4 import BeautifulSoup import requests u ...

  2. Node.js 网页爬虫再进阶,cheerio助力

    任务还是读取博文标题. 读取app2.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块, ...

  3. Node.js 网页瘸腿稍强点爬虫再体验

    这回爬虫走得好点了,每次正常读取文章数目总是一样的,但是有程序僵住了情况,不知什么原因. 代码如下: // 内置http模块,提供了http服务器和客户端功能 var http=require(&qu ...

  4. Node.js 网页瘸腿爬虫初体验

    延续上一篇,想把自己博客的文档标题利用Node.js的request全提取出来,于是有了下面的初哥爬虫,水平有限,这只爬虫目前还有点瘸腿,请看官你指正了. // 内置http模块,提供了http服务器 ...

  5. perl的几个小tips

    1.字符串比较 if($str eq "hello"){ ... } 字符串比较必须用eq,用==只适合标量 2.字符串连接 $str3=$str1."-".$ ...

  6. SUSE linux升级perl及openssl

    一.perl安装: 1.下载并解压软件:tar zxvf perl-5.24.0.tar.gz 2.运行./configure.gnu -help查看帮助,运行./configure.gnu -des ...

  7. perl代码调试

    perl调试教程 一.DESCRIPTIONA (very) lightweight introduction in the use of the perl debugger, and a point ...

  8. iOS/Swift Tips 1

    1.重写hitTest方法,干预iOS事件传递过程 如下所示,view上有一个button,button一半的frame在父类view bounds之外, 按照iOS系统默认的处理逻辑, 如果点击按钮 ...

  9. perl 对ENV环境变量的使用

    1.hash 方式访问. %ENV  key为环境变量名,value为环境变量值 2.调用ENV模块 . use Env qw(PATH); print "path is $ENV{path ...

随机推荐

  1. vs修改快捷键

    https://jingyan.baidu.com/album/9158e0006e10d8a254122826.html?picindex=1 https://sanwen8.cn/p/114IrR ...

  2. HDU1541 经典树状数组

    HDU1541 题意: 如图,等级为0的点有1,等级为1得点有4,2  等级为2的点有3,等级为3的点有5-------即即左下角的点的个数 现给你一些点(x,y),输入顺序按y升序,y相等时按x升序 ...

  3. Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

  4. linux总结shell

    一.Shell脚本常用的头部格式: 头部的作用就是告知linux此脚本的类型: 常用的头部格式如下:(/bin/bash,是bash的路径,如果不知道路径可以通过which bash进行查看,其它命令 ...

  5. linux 内存查看方法:meminfo\maps\smaps\status 文件解析

    linux 下面查看内存有多种渠道,比如通过命令 ps ,top,free 等,比如通过/proc系统,一般需要比较详细和精确地知道整机内存/某个进程内存的使用情况,最好通过/proc 系统,下面介绍 ...

  6. 华为上机测试题(数字字符串转二进制-java)

    PS:此题满分,可参考 /*  * 题目:数字字符串转二进制 * 描述: 输入一串整数,将每个整数转换为二进制数,如果倒数第三个Bit是“0”,则输出“0”,如果是“1”,则输出“1”. 题目类别: ...

  7. css3实现图片划过一束光闪过效果

    该效果在京东里的图片有. .img { display:block; position: relative; width:800px; height:450px; margin:0 auto; } . ...

  8. asp.net 网站模板怎么用,就是16aspx上面下下来的模板,里面有个sln文件,其他全是文件夹的东西

    .net写的程序模板一般都被写死了.那样只有通过程序改了.

  9. UNION UNION-ALL

    The UNION ALL operator may be what you are looking for. With this operator, you can concatenate the ...

  10. socket 和 webservice 的区别和比较

    时间紧迫,我就直奔主题. 目前需要说服客户使用webservice 而不是socket. 我觉得要先分别解释下什么是socket 什么是webservice..这个要我该怎么说才比较形象,让人一定就明 ...