perl程序中shift的用法

分类:perl
2013-08-01 10:22 阅读(?)评论(0)

shift ARRAY
shift

这个函数把数组的第一个值移出并且返回它,然后把数组长度减一并且把所有的东西都顺移。如果在数组中不再存在元素,它返回 undef。

如果省略了 ARRAY,那么该函数在子过程和格式的词法范围里移动 @_;
它在文件范围(通常是主程序)里移动 @ARGV。

子过程通常以拷贝它们的参数到词法变量里开始,而 shift 可以用于这个目的:

sub marine {
      my $fathoms = shift;      # 深度
      my $fishies   = shift;   # 鱼的数量
      my $o2           = shift;   # 氧气问题
      # ...
   }

总之就是shift没有数组作为参数时,就是移动@_这个默认的参数。
perl里经常用这种缺省方法的。

 

target region capture:

#!/usr/bin/perl -w
use strict;

my $target=shift;
my $bed=shift;
open IN,"$bed/ex_region.sort.bed" || die $!;
open OUT,">$target/ex_region.sort" || die $!;
while(<IN>)
{
        chomp;
        my @temp=split /\s+/;
        my $start=$temp[1]+1;
        print OUT "$temp[0]\t$start\t$temp[2]\n";
}
close IN;
close OUT;

http://zhidao.baidu.com/question/85574641.html

 

  最后修改于 2013-08-01 10:24    阅读(?)评论(0)
作者已禁止网友对该文进行评论