- 论坛徽章:
- 0
|
本帖最后由 alphafighter 于 2015-11-09 23:17 编辑
回复 4# stanley_tam
代码很厉害。不过代码里面的意义我不是很明白,请您帮我解释一下~
#!perl
use strict;
use warnings;
my $filename = 'dummy.pepmasses'; 这里为什么不先开始文件读取而是先开始输入?这是因为个人习惯还是这样有益程序运行?
my $from = 0;
my $to = 0;
print "What range of information do you want to know ?\n";
{
my $str = <STDIN>;
chomp $str;
($from, $to) = split /\.\./, $str; 这里是user输入的区间,比如(10,100), 用户输入的是xx..xx这个split命令的作用怎么理解?
}
# open file
my $ifile_fh;
open $ifile_fh, '<', $filename or
die "Cannot open file $filename: $!\n";
# skip header
readline $ifile_fh;
my $prepnumber = 0;
while (defined (my $line = <$ifile_fh>) ) {
# read each line in file
chomp $line;
my $mz_value = (split /\s+/, $line)[2]; 这句话的意思就是给一个变量赋予第二列的一个值?
if ($mz_value >= $from and $mz_value <= $to) { 当这个if开始后,perl就会对每一行进行这个区间的比对,然后每个属于
# print matched line 这个范围的行都会被打出来,这个if会不断重复直到读取完最后一行文件
print "$line\n"; 是这样吗?
++$prepnumber;
}
}
print "prepnumber=$prepnumber\n" if $prepnumber; 最后的这个 if $prepnumber是什么意思?
close $ifile_fh;
__END__
这个程序是一个大的程序的一部分。也就是说当用户输入完成一个值,程序返回一个值后,程序应该是等待用户输入下一个值或者进行到下一部分的,但是运行完一遍后每个变量里面都已经有了值。当close $ifile_fh运行的时候,所有的变量都被清零了吗?
如果我想增加一个功能,除了匹配完后输出匹配的行,所有匹配行的的M/Z值要全部加起来求一个平均数,应该怎么做?
这是我想的可能的代码:
my $prepnumber = 0;
my $summass = 0;
while (defined (my $line = <$ifile_fh>) ) {
# read each line in file
chomp $line;
my $mz_value = (split /\s+/, $line)[2];
if ($mz_value >= $from and $mz_value <= $to) {
# print matched line
print "$line\n";
++$prepnumber;
$summass = $mz_value+$summass;
}
}
print "prepnumber=$prepnumber\n" if $prepnumber;
print "average m/z number = $summass/$prepnumber\n";
close $ifile_fh;
这样对吗?
运行后最后报错 Use of uninitialized value $mz_value in numeric ge (>=) at 3.pl line 29, <$file_fh) line 423.
输出的结果正常。我运行的是 perl 5,version 22, subversion 0 (5.22.0) built for MSWin32-x64-multi-thread
还有在帖子内输入的这种带编号,灰色背景的代码是怎么做到的?
我拷贝了你的代码,但是粘贴在帖子上就变成了什么都没有的状态。
问题有点多哈。好不容易逮到个高手不容易啊 ^_^ |
|