总结:

  • q()、qq()、qw(同单引号)、qx{牢记是花括号},分别是单引号、双引号、创建字符串列表 和 捕获命令输出。
  •  

    第9学时 其他函数和运算符

    一件事情可以使用多种方法完成。

    有哪些其他的函数和运算符?

    index()、rindex()、substr()、tr///、printf()、堆栈:push()、pop()、shift()、unshift()。splice()。

    本节主要内容:

  • 对标量进行字符串搜索(之前用正则表达式)
  • 字符替换
  • print函数
  • 数组作为堆栈和队列
  • 如何在一个标量中搜索字符串?

    index函数从左边开始找,0即是最左边的字符,没有找到则返回-1,被搜索的字符串可以是标量或表达式。

    #函数可以没有括号,但参数之间必须有逗号。
    index "Ring around the rosy", "around";
    index("Pocket full of posies", "ket");
    $a="Ashes, ashes, we all fall down";
    index($a, "she");
    @a=qw(oats peas beans);
    index join(" ", @a), "peas";
    #可以设置搜索的起始位置
    index($reindeer, "da", 1);
    #可以遍历,找到子字符串的所有位置
    $source="One fish, two fish, red fish, blue fish.";
    $start=0;
    #设置搜索起始位置,使用循环更新下次搜索位置,本质就是一个迭代
    while(($start=index($source, "fish", $start)) != -1){
    print "Found a fish at $start\n";
    $start++;
    }

    用rindex向后搜索

    $a="She loves you yeah, yeah, yeah.";
    rindex($a, "yeah");
    rindex($a, "yeah", 25);

    用substr分割标量

    从起始位置offset开始,向前或向后取length个字符(没有length就取到结尾)。

    $a="I do not like green eggs and ham";
    print substr($a, 25);
    print substr($a, 14, 5);
    #可以利用substr函数进行标量的部分替换
    substr($a, 0, 3) = "li";
    #$a中的0到3,被替换为“li”

    翻译运算符(转换运算符) 类似s/pattern/replacement/,有时会用到连接运算符(=~)

    #与s///不同,tr是分别翻译,A翻译为X,之后同理
    $r=~tr/ABC/XYZ/;

    格式化输出 printf函数

    #默认是输出到STDOUT文件句柄
    #逗号放在哪是门学问,句柄后不能有逗号,参数间必须有逗号
    printf formatstring, list;
    printf filehandle formatstring, list;

    %域说明符,(%-w.dx)

    w是总宽度,d是小数点左边的位数(for数字)或总宽度(for字符串),x是数据类型。其中%和x必不可少。

    printf("%20s", "Jack");
    $amt=7.12;
    printf("%6.2f", $amt);
    printf("%c", 65);

     

    堆栈形式的列表

    将横向数组设想为一个纵向堆栈。

    顶部:push函数压入栈顶;pop函数弹出栈顶元素

    底部:shift函数添加底部;unshift函数从底部取出元素

    (注意:一次只能删除一个元素;但是一次可以添加一个数组和列表)(没有指定堆栈,默认操作 @_ 或 @ARGV)

    #堆栈的底部是元素0,堆栈的顶部是数组中最后一个元素
    @band=qw(trombone);
    push @band, qw(li zhi); $brass=shift @band; #取出栈尾元素,即第一个元素
    $wind=pop @band; #弹出数组最后元素,即栈顶 unshift @band, "harmonica"; #添加

    splice函数操作数组

    splice array, offset, length, list; #其中length和list可以不写

    splice函数用于删除指定元素,length指定长度,list则是替换的数组。

     

    第10学时 文件与目录


  • 获得目录列表
  • 创建和删除文件
  • 创建和删除目录
  • 获取文件信息
  • 有哪些函数?

    opendir()、readdir()、closedir()、grep()、glob()、cwd()、chdir()、mkdir()、rmdir()、unlink()、rename()、stat。

    如何获得目录列表?(打开目录 读取目录) 此步的局限性:只能查看指定目录下的文件目录列表信息

    打开文件 和 打开目录 不太一样,打开目录 是为了获得目录里的信息列表,而不能写入或修改。

    # 打开目录
    opendir(DIR, './') or die $!;
    # 以标量形式读取目录
    while(readdir(DIR)){
    print $_."\n";
    }
    # 以数组形式读取目录
    @dir = readdir(DIR);
    foreach (@dir){
    print $_."\n";
    }
    # 目录句柄用完之后必须关闭
    close(DIR);
    # 通常会用grep过滤掉(.)和(..)目录
    @files = grep(!/^\.\.?$/, readdir(DIR));

     

    如何浏览目录、切换目录、创建目录、删除目录?

    如何读取子目录?

    # 使用Cwd包,获取当前工作目录
    use Cwd;
    print cwd, "\n";
    chdir '../' or warn $!; # 改变当前目录
    print getcwd;
    print "DIrectory to create?";
    my $newdir = <STDIN>;
    chomp $newdir;
    mkdir($newdir, 0755) or die $!; # 创建目录,权限为755
    rmdir($newdir) or die $!; #删除目录

    如何删除文件?给文件改名?

    unlink <*.bat>;  #删除匹配文件
    $erased = unlink 'old.exe', 'a.out', 'personal.txt'; #删除列表文件,并返回已删除数量
    unlink @badfiles; #删除列表内文件
    unlink; #删除$_内文件
    if (!rename "myfile.txt", "archive.txt"){
    warn $!;
    }

     

    第11学时 系统之间的互操作性


  • system()函数
  • 捕获输出
  • 可移植性
  • 有哪些内容?

    system()、(>)、(|)、(&)、(``)、qx{}、use strict、$^O。

    system()函数用法与open()相反,成功返回0,错误返回非零值。(不能用or来输出异常)

    system("dir");

    perl会调用shell或command,所以支持命令的重定向(>),管道(|) 和 后台操作任务(&)。

    system("dir > faqfile.txt");
    system("sort $f | less");
    system("Xterm &");

     

    system()函数无法捕获命令输出,绕圈子的做法是:

    system("dir > outfile");
    open(OF, "outfile") or die $!;
    @data = <OF>;
    close(OF);
    foreach(@data){
    print "$_\n";
    }

    perl中专门捕获输出的语法:(结果可以赋给标量,也可以赋给列表)

    $directory = `dir`;
    $directory_2 = qx{dir};
    print $directory;
    @dir = `dir`;
    foreach (@dir){
    print "$_"
    }

    管道

    dir > outfile;
    sort outfile > newfile;
    more newfile; dir | sort | more;

     

    第12学时 使用P e r l 的命令行工具


     

     

     

     

     

     

     

    第13学时 引用与结构


     

     

     

     

     

     

    第14学时 使用模块


    perl的核心特性体现在:正则表达式 和 外部程序接口。

  • 如何使用模块
  • 内置模块
  • 核心模块列表
  •  

     

     

     

     

     

    第15学时 了解程序的运行性能


     

     

     

     

     

    第16学时 P e r l 语言开发界


     

     

     

     

     

     

     

    其他:

    use Cwd 'abs_path';   #获取指定文件和目录的绝对地址
    use warnings;
    $myfile= "fastq_R1.txt";
    $mydir="./1";
    print(abs_path($myfile)."\n"); #使用abs_path函数
    print(abs_path($0)."\n");
    print(abs_path($mydir)."\n");

    以下是语法在只读模式打开file.txt的。这里小于< signe 指示,文件必须以只读模式运行结束

    open(DATA, "<file.txt");

     

    参考资料:

    Perl范围变量声明: my,our,local

    perl一次读取多行并按条件输出
    perl split()函数详解
    notepad++正则表达式
     
     
    
    
     

    最新文章

    1. JavaScript-location:封装当前窗口正在打开的url的对象
    2. iOS 中的frame,bounds,center,transform关联
    3. mysql存储过程和事件
    4. 2013-2014集训之DP
    5. 李洪强iOS开发Swift篇—02_变量和常量
    6. source install MacPorts--checking for Tcl configuration... configure: error: Can&#39;t find Tcl configuration definitions
    7. My Solution: Word Ladder
    8. Java 集合 散列表hash table
    9. vm选项大全
    10. iOS开发——WAVE音频文件解析
    11. 关于如何介绍spring框架。
    12. 通过数据绑定控制WPF动画启动,WPF动画开始
    13. c# 静态构造函数与构造函数的调用先后
    14. fastdfs(https://www.jianshu.com/p/1c71ae024e5e)
    15. 深入理解,函数声明、函数表达式、匿名函数、立即执行函数、window.onload的区别.
    16. 重写对象ToString方法
    17. Upload文件时出现&quot;Cannot access a closed file&quot;错误
    18. 用Omniauth来Login with Facebook(Go-rails课程)
    19. jenkins systemctl启动失败
    20. 关于MySql8.X设置允许root远程登陆的问题

    热门文章

    1. size函数
    2. EasyUI 格式化DataGrid列
    3. ascii转int,int在转回ascii原值
    4. android 入门 006(sqlite增删改查)
    5. MVC 中DropDownList 用法
    6. 回车和换行在linux下和windows下
    7. 给table设置滚动条
    8. acdream1197 Points In Cuboid(hash树状数组)
    9. FreeSWITCH第三方库(音频)的简单介绍(一)
    10. Servlet&amp;jsp基础:第一部分