I am trying to create a program in Perl that will read through thousands of characters that and attempt to find a matching string of characters. I need to print out the string plus the next five letters. I also need to print out the location of where it was found, i.e. how many letters in. I am pretty new to Perl. I’m just learning it in class now.
Here is the code I have so far:
#!/usr/bin/perl
$sequence = 'abcd';
$fileName = 'file.txt';
#Opening file
unless (open(fileName, $fileName)) {
print "Cannot open file.";
exit;
}
@tempArr = <fileName>; #Adding the lines to an array
close fileName; #closing the file
$characters = join('', @tempArr); #making it a clean string
$characters =~ s/\s//g; #removing white lines
if (characters =~ m/$sequence/i) {
#Print $sequence and next five characters
}
else {
print "Does not contain the sequence.";
}
exit;
file.txt will look like:
aajbkjklasjlksjadlasjdaljasdlkajs
aabasdajlakjdlasdjkalsdkjalsdkjds
askdjakldamwnemwnamsndjawekljadsa
abcassdadadfaasabsadfabcdhereeakj
I need to print out the “abcdheree”
To print
$sequence& the 5 characters after it, try using :(You forgot the
$oncharacters)NOTE
.mean any character{5}is a quantifieropen, use 3 arguments like :open my $fh, "<", "$file" or die $!;See http://perldoc.perl.org/perlopentut.htmluse strict; use warnings;at the top of your script$on variables (you miss many ones)myfor declaring variablesforeach my $line (@tempArr) { #process $line }@melTemp1that is never declaredFINALLY