Garbage value

I write a program to find a palindromic region in given sequences. but it dosen't seems to be run well. please give me your suggestions

INPUT: AGCTAGCTCGAAGGTAG
code is
here

#!/usr/bin/perl 
#Palindromic sequence  
 
 
print "enter the sequence:\n"; 
$rna = <STDIN>; 
 
chomp $rna;       
 
#@rna =split ('',$rna); 
 
for($i=0;$i<=length($rna)-5;$i++){ 

  $s=substr($rna,$i,5); 
  $s2= reverse($s); 

  $s2 =~ tr/ACGTacgt/TGCAtgca/; 
  #print "$s2\n"; 
  if($rna =~ /$s2/){ 
    print "palindrome found: $s\n"; 
    exit; 
  }
}

:confused::confused:

I'd test it character by character instead of testing long strings. I'm not too fluent in perl, but hopefully the logic on this should be followable:

$ cat palindrome.awk
BEGIN { FS=""; ORS=""; }

{
	for(N=1; N<=NF; N++)
	for(M=N+1; M<=NF; M++)
	{
	# If it does not begin and end on the same char, not a palindrome
		if($M != $N) continue;

		A=N ; B=M; P=1;

		# Test towards the middle until A and B cross
		while(P && (B > A))
			if($(B--) != $(A++) ) P=0;

		if(!P) continue;	# Not a palindrome

		printf("Palindrome, %d-%d: ", N, M);
		for(A=N; A<=M; A++) print $A;
		printf("\n");
	}
}
$ echo "AGCTAGCTCGAAGGTAG" | awk -f palindrome.awk
Palindrome, 5-11: AGCTCGA
Palindrome, 6-10: GCTCG
Palindrome, 7-9: CTC
Palindrome, 10-13: GAAG
Palindrome, 11-12: AA
Palindrome, 13-14: GG
$
1 Like

thanks for your help but I have to do this problem solely by using perl script. I am new learner in perl so please help me to identify the problem in code written by me.

I can't tell what you're trying to do. Why do you have fixed strings like ACGTacgt in there?

---------- Post updated at 11:00 PM ---------- Previous update was at 10:53 PM ----------

On closer examination, I think I see part of the problem. reverse() doesn't reverse a string, it reverses a list:

# prints abcdefgh
print "abcd", "efgh", "\n";
# prints efghabcd
print reverse("abcd", "efgh"), "\n";

So your 'reversed' string is actually identical..

You appeared to have the right idea earlier with split(). It outputs a list which reverse() can use, which also outputs a list, which you can cram into join() to make it a string again.

$r=join('', reverse(split('', $s)));
if($r == $s) { ... } # Same backwards and forwards is a palindrome