LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-18-2008, 05:24 AM   #1
pwd_pwd_omg_pwd
LQ Newbie
 
Registered: Oct 2008
Location: Cardiff, Wales
Distribution: Jaunty
Posts: 20

Rep: Reputation: 0
perl: simple one line replacement, simple error message!


Just been trying out some simple perl to get me started, straight from a book and i keep getting back a syntax error back for the replacement line in:
Code:
sub translate {
	my ($mrna) = @_;
	my $pro = " ";
	while ($mrna =~s/(...)//){
		$pro = $pro . $codonMap{$1};
	}
	return $pro;
}
in the book ive got it shows the ~ as superscript but ive been assured that that key doesnt exist so not sure why it doesnt like it.
 
Old 11-18-2008, 06:20 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
Seeing the complete regex used in the substitution, along with the kinds of input the function is expected to take and the expected output, might help.

Last edited by taylor_venable; 11-18-2008 at 06:28 AM.
 
Old 11-18-2008, 07:33 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
It wouldn't hurt to post the actual error message. As a general tip, try running the script with use diagnostics; in effect. You can put that up at the top of your script instead of use warnings; - it will give you more verbose error messages. Or you can run the script this way:
Code:
perl -Mdiagnostics script_name
 
Old 11-18-2008, 05:44 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
Always use

use strict;
use warnings;

at the top of your programs it'll help a lot. BTW, an alternate for 'use warnings;' is to use the -w switch as in

#!/usr/bin/perl -w

as the first line of your code (adjust to match your env).
In this case,

use diagnostics;

as well is good for debugging. Showing us the exact error msg and the input data would really help.
 
Old 11-19-2008, 08:55 AM   #5
pwd_pwd_omg_pwd
LQ Newbie
 
Registered: Oct 2008
Location: Cardiff, Wales
Distribution: Jaunty
Posts: 20

Original Poster
Rep: Reputation: 0
error message:

The error message reads as this:

Code:
syntax error at transprog.pl line 18, near "while ($mrna ~=s/("
  (Might be a runaway multi-line // string starting on line 10)
Global symbol "$pro" requires explicit package name at transprog.pl line 18.Global symbol "$pro" requires explicit package name at transprog.pl line 18.Execution of transprog.pl aborted due to compilation errors.
and it is still saying it after I have changed the replacement line, which i thought was the whole problem. the use warnings didnt say anything new than it was before and the -Mdiagnostics said: its probably a syntax error.

The script has a few other parts and subs to it, but essentially this sub will take an inputed mRNA seq AGCUAGUCGCUGCAGUCUGCAUGC... and look up each 3 letters to a data set at the end of the format:

Ala GCU, GCC, GCA, GCG
Leu UUA, UUG, CUU, CUC, CUA, CUG
Arg CGU, CGC, CGA, CGG, AGA, AGG
Lys AAA, AAG
etc.
and list the amino acids.
AlaLeuArgLys...

The first part of the code reads:
Quote:
#!/usr/bin/perl
use strict;
use diagnostics;
my %codonMap;

## translates DNA strings to RNA
sub transcribe {
my ($dna) = @_;
my $rna = scalar reverse $dna;
$rna =~tr/ACGT/UGCA; ##10
return $rna;
}

## translates mRNA strings to proteins
sub translate {
my ($mrna) = @_;
my $pro = " ";
while ($rna ~=s/(...)//){ ##18
$pro = $pro . $codonMap{$1};
}
return $pro;
}
(10 and 18 are the lines it mentions in the error btw)

thanks for the help guys.

Last edited by pwd_pwd_omg_pwd; 11-19-2008 at 09:00 AM. Reason: missed a bit out.
 
Old 11-19-2008, 11:49 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by pwd_pwd_omg_pwd View Post
The error message reads as this:

Code:
syntax error at transprog.pl line 18, near "while ($mrna ~=s/("
  (Might be a runaway multi-line // string starting on line 10)
Global symbol "$pro" requires explicit package name at transprog.pl line 18.Global symbol "$pro" requires explicit package name at transprog.pl line 18.Execution of transprog.pl aborted due to compilation errors.
and it is still saying it after I have changed the replacement line, which i thought was the whole problem. the use warnings didnt say anything new than it was before and the -Mdiagnostics said: its probably a syntax error.

The script has a few other parts and subs to it, but essentially this sub will take an inputed mRNA seq AGCUAGUCGCUGCAGUCUGCAUGC... and look up each 3 letters to a data set at the end of the format:

Ala GCU, GCC, GCA, GCG
Leu UUA, UUG, CUU, CUC, CUA, CUG
Arg CGU, CGC, CGA, CGG, AGA, AGG
Lys AAA, AAG
etc.
and list the amino acids.
AlaLeuArgLys...

The first part of the code reads:

(10 and 18 are the lines it mentions in the error btw)

thanks for the help guys.
Try to make our lives easier - post the code processed by 'cat -n', so we'll be able to see line numbers and related them to the ones in error messages.

Last edited by Sergei Steshenko; 11-19-2008 at 11:55 AM.
 
Old 11-19-2008, 11:53 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
deleted by Sergei.
 
Old 11-19-2008, 11:57 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by pwd_pwd_omg_pwd View Post
The error message reads as this:

Code:
syntax error at transprog.pl line 18, near "while ($mrna ~=s/("
  (Might be a runaway multi-line // string starting on line 10)
Global symbol "$pro" requires explicit package name at transprog.pl line 18.Global symbol "$pro" requires explicit package name at transprog.pl line 18.Execution of transprog.pl aborted due to compilation errors.
and it is still saying it after I have changed the replacement line, which i thought was the whole problem. the use warnings didnt say anything new than it was before and the -Mdiagnostics said: its probably a syntax error.

The script has a few other parts and subs to it, but essentially this sub will take an inputed mRNA seq AGCUAGUCGCUGCAGUCUGCAUGC... and look up each 3 letters to a data set at the end of the format:

Ala GCU, GCC, GCA, GCG
Leu UUA, UUG, CUU, CUC, CUA, CUG
Arg CGU, CGC, CGA, CGG, AGA, AGG
Lys AAA, AAG
etc.
and list the amino acids.
AlaLeuArgLys...

The first part of the code reads:

(10 and 18 are the lines it mentions in the error btw)

thanks for the help guys.
You apparently have missing trailing '/' here:

$rna =~tr/ACGT/UGCA;

, i.e. I think it should be

$rna =~tr/ACGT/UGCA/;
 
Old 11-19-2008, 12:53 PM   #9
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Two other problems that I can see: on line 18 (1) you've written ~= instead of =~ and (2) you're using $rna in a subroutine where it isn't visible. Did you mean $mrna there (from line 16) or did you want $rna to be available to both subroutines?

As a general rule, the line number of the error from Perl is where the error gets caught. You will need to backtrack up from there, since the real problem is (often) somewhere earlier. Here's a quick example:
Code:
#!/usr/bin/perl
use strict;
use warnings;

my $foobar

print "$foobar\n";
The problem is the missing semicolon in line five, but Perl will tell me that the error was caught at the print statement in line 7 (even though the print statement itself is fine).

Last edited by Telemachos; 11-19-2008 at 12:55 PM.
 
  


Reply

Tags
perl, replacement


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mplayer shows error message [AO_ALSA] Unable to find simple control 'PCM',0 sagsriv Linux - Newbie 4 08-11-2008 08:49 AM
Simple bash script "unexpected end of line error" snowman81 Programming 11 11-11-2007 09:31 AM
simple regex not so simple (perl) ludeKing Programming 5 03-02-2005 02:29 AM
PERL error trying to run simple HTML page.... vous Programming 1 08-25-2003 09:01 AM
Simple Perl prog. getting Error 500 message. XxAndyxX Programming 1 07-06-2003 11:25 AM


All times are GMT -5. The time now is 08:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration