CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Substring Issue


New User

Mar 26, 2015, 1:05 PM

Post #1 of 2 (6345 views)
Substring Issue Can't Post

Every time I run the commands below, I get a "Global symbol "$substring" requires explicit package name at Line 62"..

I am searching for the number of sequences (within the hash), but for some reason, my "$substring" function keeps giving me an error.

Here is the entire Perl script:

use strict;
use warnings;

my %codon; # hash to store amino acid residues, keyed by codons

# open the codon file and read in the lines, to store them in a hash
open (CODON, "<" . $ARGV[1]) or die $!;
while (<CODON>) {

my @line= split(/,/, $_); # split the line on commas
$codon {$line[0]} = $line[1];
close (CODON);

open (GENES, "<" . $ARGV[0]) or die $!;

# start the offset at 0 and the length of a codon is 3 letters long
my $offset= 0;
my $length= 3;
my $geneID= "";
my $hello;

while (<GENES>) {

# check if we are in a gene ID or sequence line using a regular expression
if ($_ =~ /AT(C|M|\d)G\d+/){
$geneID = $_;
print $_ . "\n";
} else {
# keep translating as long as there are codons remaining AND
# we have not encountered a stop codon
my $stopped= "no";
while ($offset <= (length ($_) - $length) and $stopped eq "no") {
my $substring = substr($_, $offset , $length);
print $codon{$substring};

if ($codon{$substring} eq "*") {
$stopped= "yes";

$offset = $offset + 3; # move to the next "codon"
#add substrings (amino acids) together
$hello = $_ . $codon{$substring};

$offset= 0; #reset

print "\n";

close (GENES);

# Make a hash table (with geneID in one column, and the gene codon in the other),
# then search the translated ORFs (genes) for the presence of the (ordered) motifs
my %protein;
$protein{$geneID} = $codon{$substring};
if ($protein{$geneID} =~ /motif...not shown/) {
my $count_1++;
print $count_1;

if ($protein{$geneID} =~ /[motif...not shown]/) {
my $count_2++;
print $count_2;

if ($protein{$geneID} =~ /motif...not shown/) {
my $count_3++;
print $count_3;

if ($protein{$geneID} =~ /motif...not shown/) {
my $count_4++;
print $count_4;

if ($protein{$geneID} =~ /motif...not shown/) {
my $count_5++;
print $count_5;

Thank you very much in advance!!

(This post was edited by alkawi on Mar 26, 2015, 2:10 PM)

Veteran / Moderator

Mar 26, 2015, 1:43 PM

Post #2 of 2 (6341 views)
Re: [alkawi] Substring Issue [In reply to] Can't Post

$substring was declared in the while loop and after that while loop finishes, that var is no longer available. If you need to use that var outside of the while loop, then you'll need to declare it prior to the loop so you can use it in line 62.

I have not looked over your code logic, so I can't say exactly what other changes you need to make to get your script working correctly.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives