Hi Perlmonks,
Below is a code i'm testing. I am a beginner at Perl and saw that the $` special variable refers to the string preceding a match. I wanted to try using it to print a line previous to a matched string.
If this is not possible then any advice on how to print a string previous to a matched string would be very welcome. The problem is I get the following warning and can't figure out how to fix it
Use of uninitialized value in pattern match (m//) at test.pl line 9, <FH> line 1.
Use of uninitialized value in pattern match (m//) at test.pl line 9, <FH> line 2.
Use of uninitialized value in pattern match (m//) at test.pl line 9, <FH> line 3.
Use of uninitialized value in pattern match (m//) at test.pl line 9, <FH> line 4.
Use of uninitialized value in pattern match (m//) at test.pl line 9, <FH> line 5.
the contents of the file are (i'm only using this file to test the script, normally these files could have several strings of letters with each having a title line beginning with >)
>random_seq
atggtccgtgatcagcatcctttggggaatccacatttattcggaccacctccggattcgtcagcatgac
+agctggtcgugguuuaguaugatgtgatgcgatcatgtacatcaggatttaggaggccctccactgcat
+cgtcgactagacgatcagttagggaaaacgtaugguugugatggtacatgacggaccacacgccgactt
+agcatttgttacagtagcgtgattgacatguuguuggug
I want to test the string of letters for a motif and if found print the line beginning with '>' (i.e. the line/string before the match). finally, here's the code
<code> #!usr/bin/perl
use strict;use warnings;
my $filename = $ARGV[0];
open (FH, $filename) || die "Cannot open file\n";
while (<FH>)
{
if (my $line =~ m/
A-Za-z/)
{
print $`;
}
}
close FH;
<code>
would appreciate any help you have to offer
GrandFather added code tags to break long line.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.