I have some realy simple code that just doesn't seem to work when I try using a variable regex. Literal regex works fine.

Here's my code:

#!/usr/bin/perl -w my $seq; open (SEQ, "<$ARGV[0]"); open (REP, "<$ARGV[1]"); open (OUT, ">$ARGV[2]"); while (<SEQ>){ $seq = $_; while (<REP>){ print OUT if /$seq/; } } close (SEQ); close (REP); close (OUT);
Right now I am just getting an empty file for an output. If I replace /$seq/ with /AAA/ or any other literal, it works just fine.

I'm sure its something easy, but I've been googling all afternoon to no avail.

Here are examples of the contents of the SEQ and REP files:

SEQ: AELIVQPELK REP: 1 116.68 116.68 48.2199996709824 26.8999993801117 21.17 +99994111061 ENST00000379802_141 [201 - 8954] cdna:known chromos +ome:GRCh37:6:7541808:7586950:1 gene:ENSG00000096696 gene_biotype:prot +ein_coding transcript_biotype:protein_coding 2 99.00000 +09536743 AELIVQPELK -5.64623987884261E-05 1138.65991 +210938 570.3372 1138.65979003906 570.337158203125 2 14 + 2.1.1.3480.1 1 0.7224 -1 -1
Thanks for your help!

Update: Got what I wanted with the following code. Thanks for the input everyone, it was helpful. I was unaware of the mechanics of a while loop using an open file as a condition. I'm sure its still a little messy but I'm a biologist not a programmer, so what can you expect?

#!/usr/bin/perl -w use strict; my (@seq, @rep, $i, $n, $l, $t); open (SEQ, "<$ARGV[0]"); open (REP, "<$ARGV[1]"); open (OUT, ">$ARGV[2]"); $i=0; $n=0; $l=0; while (<SEQ>){ chomp(); $seq[$i] = $_; $i++; } while (<REP>){ $rep[$n] = $_; $n++; } while ($l < $i){ $t=0; while ($t < $n){ print OUT $rep[$t] if $rep[$t] =~ /$seq[$l]/; $t++; } $l++; } close (SEQ); close (REP); close (OUT);

In reply to Simple filter from rows of input file by ablakely

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.