Perl子程序

发布于 2024-12-14 03:59:42 字数 1486 浏览 0 评论 0原文

我在执行 Perl 子例程时遇到问题。 这是定义:

sub primer{
     print STDERR "primer is $_[0]\n";
    $primer=$_[0];
   if ($_[0]=~/ATTACCGC/){
           mkdir ("Primer1") || die "Unable to create directory <$!>\n";
           open OUTFILE1,">","Primer1/lt450";
           open OUTFILE2,">","Primer1/no_primer_lt450";
           open REPORT,">","Primer1/Report";
           &primer_analysis('ATTACCGC');}

           if ($_[0]=~/CCGTCAATTC[AC]/){
           mkdir ("Primer2") || die "Unable to create directory <$!>\n";
           open OUTFILE1,">","Primer2/lt450";
           open OUTFILE2,">","Primer2/no_primer_lt450";
            open REPORT,">","Primer2/Report";
           &primer_analysis('CCGTCAATTC[AC]');}

这是上面应该调用的另一个子例程的一部分:

sub primer_analysis{
     $primer=$_[0];
    while ($line = <INFILE>){
         if ($line =~ /^>/) {
             $header = $line;
            $headcnt++;}
         if ($line !~/^>/){
         $seq = $line;
          chomp($seq);
        if (length($seq)<450 && $seq=~/^$primer/){
            $len_450=length($seq);
            $TB_450=$len_450+$TB_450;
               $cnt450++;
              print OUTFILE1 "$header";
              print OUTFILE1 "$seq\n";}}

以下是调用该函数:

&primer('ATTACCGC');
&primer('CCGTCAATTC[AC]');

问题是,当我运行程序时,仅执行 Primer1,即正确创建目录和数据,但是没有任何反应到入门2。 有什么想法为什么只执行函数的一部分吗? 谢谢

I am having trouble with execution of my perl subroutines.
Here is the definition:

sub primer{
     print STDERR "primer is $_[0]\n";
    $primer=$_[0];
   if ($_[0]=~/ATTACCGC/){
           mkdir ("Primer1") || die "Unable to create directory <$!>\n";
           open OUTFILE1,">","Primer1/lt450";
           open OUTFILE2,">","Primer1/no_primer_lt450";
           open REPORT,">","Primer1/Report";
           &primer_analysis('ATTACCGC');}

           if ($_[0]=~/CCGTCAATTC[AC]/){
           mkdir ("Primer2") || die "Unable to create directory <$!>\n";
           open OUTFILE1,">","Primer2/lt450";
           open OUTFILE2,">","Primer2/no_primer_lt450";
            open REPORT,">","Primer2/Report";
           &primer_analysis('CCGTCAATTC[AC]');}

Here is part of another subroutine that should be invoked by above:

sub primer_analysis{
     $primer=$_[0];
    while ($line = <INFILE>){
         if ($line =~ /^>/) {
             $header = $line;
            $headcnt++;}
         if ($line !~/^>/){
         $seq = $line;
          chomp($seq);
        if (length($seq)<450 && $seq=~/^$primer/){
            $len_450=length($seq);
            $TB_450=$len_450+$TB_450;
               $cnt450++;
              print OUTFILE1 "$header";
              print OUTFILE1 "$seq\n";}}

And following is calling the function:

&primer('ATTACCGC');
&primer('CCGTCAATTC[AC]');

problem is that when I run the program only the Primer1 gets executed i.e. directories and data is properly created, however, nothing happens to Primer2.
Any ideas why only one part of the function is getting executed?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2024-12-21 03:59:42

字符[]是正则表达式中的特殊字符。您需要在子引物中通过编写 CCGTCAATTC\[AC\] 来转义它们,或者简单地使用 eq 字符串比较。事实上,您的第二个 if 失败,因为底漆中的文字 [[AC] 字符类不匹配。

the characters [] are special characters in regular expressions. You need to escape them in sub primer by writing CCGTCAATTC\[AC\], or simply use the eq string comparison. As is, your second if is failing because the literal [ in your primer does not match the [AC] character class.

_失温 2024-12-21 03:59:42

一般建议:

  1. 使用严格;使用警告;
  2. 考虑使用shift代替$_[0],即

    $primer = 移位;
        如果 $primer =~ /ATTACCGC/){ ..... 等等
    

    而不是

    $primer=$_[0];
        如果($_[0]=~/ATTACCGC/){
    

    ...然后在函数中使用 $primer 而不是 $_[0]。

  3. 建议使用词法文件句柄
  4. 不要使用 & 来调用您的函数。

General advice:

  1. Use strict; use warnings;
  2. Consider using shift instead of $_[0], i.e.

    $primer = shift;
        if $primer =~ /ATTACCGC/){ ..... etc.
    

    instead of

    $primer=$_[0];
        if ($_[0]=~/ATTACCGC/){
    

    ...and then use $primer instead of $_[0] in your function.

  3. Lexical filehandles are recommended.
  4. Don't use the ampersand to call your functions.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文