for example:
my input is "HCHATCABCKDLOTSHCNAGWABCHSTABCADLOS"
and i would like to detect the substring "ABC" and make my output into
"HCHATC(ABC)KDLOTSCNAGW(ABC)HST(ABC)ADLOS"
and in addition, be able to detect the position of the substring.
by using
$input=<STDIN>; #my input
$substring=<STDIN>; #my substring
how am i supposed to do the following above using loop to scan through the whole string detecting every single substring?
You can use the function substr.
Take a look at this.
You don't need to use a loop. You can use a regexp to find the substring and place the () in the string around the substring. You can use index() to find the location of the substring in the string.
Perl regular expessions tutorial
index: find a substring within a string
thanks guys, but for example
input is TTTTTTTTTTTTTTTTTTTABCTTTTTTTABCTTTTTTT
given that ABC is the phase of interest.
final output is
TTTTTTTTTTTTTTTT(ABC)TTTTTT(ABC)TTTTTTT
(printed out)
how to print the final output including those T's?
Didn't bother to read the tutorial I posted a link to? I will give you a solution this time, but I prefer to help people that show more effort than you have so far:
one way:
another way:Code:my $string = "TTTTTTTTTTTTTTTTTTTABCTTTTTTTABCTTTTTTT"; $string =~ s/([^T]+)/($1)/g; print $string;
next time post some perl code you have tried.Code:my $string = "HCHATCABCKDLOTSHCNAGWABCHSTABCADLOS"; $string =~ s/(ABC)/($1)/g; print $string;![]()
thanks for the help.
another query is when for example
WHTSABCWHNSNDYCNSYUABCHDNABC
_____4_______________19_____25
i want the know the position of all ABC lies in a string.
the result for the following position is(with the first character counted as 0)
4,19 and 25.
how am i supposed to do so? i can only get the first ABC position in the string, the rest of the ABC substring has been ignored.
Last edited by lichy; 07-04-2007 at 07:25 PM.
see the link to the index function I posted above. From now on I will not help you unless you post some perl code that you have written and need help with.
sorry to say but your index link doesnt explain much on how to capture all the position of my desired substring in a string. this is my program anyway.
$readingframe =1;
for ($i=0;$i<$readingframe;$i++)
{
{
$len = int(length(substr($sequence1, $i))/1);
for ($j=0;$j<$len;$j++)
{
$nuc = substr($sequence1, $i+$j*1,1);
if($nuc==$intseq)
{
$position = index($sequence1, $nuc);#the problem i am facing.
$arry_pos[$j]=$position;
@ary[$j]=$arry_pos[$j];
}
print "End of search.\n";
}
}
print "\nPosition at: ";
for ($j=0;$j<$len;$j++)
{
print "@ary[$j],";
}
in the end the output printed by @ary[$j], are "4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4" which is the first ABC position in the string.
Last edited by lichy; 07-04-2007 at 08:00 PM.
It's true, the index() function does not tell you how to get the position of all the substrings within the string. But it is a trivial task once you understand how to apply some basic logic to the problem:
same thing using a "while" loop:Code:my $string = 'WHTSABCWHNSNDYCNSYUABCHDNABC'; my $substring = 'ABC'; my $r = length($string); my $n = length($substring); my $x = $r - $n; my $index = 0; do { $pos = index $string,$substring,$index; print "$substring is at position: $pos\n"; $index = $pos+$n; } until ($pos >= $x);
And there are a number of other ways to code the solution to your (homework?) question.Code:my $string = 'WHTSABCWHNSNDYCNSYUABCHDNABC'; my $substring = 'ABC'; my $r = length($string); my $n = length($substring); my $x = $r - $n; my $index = 0; while ($pos < $x) { $pos = index $string,$substring,$index; print "$substring is at: $pos\n"; $index = $pos+$n; };
Don't expect the functions' documentation to tell you how to accomplish very specific tasks. It's up to you to apply the programming logic to solve your specific problem.
once again, thanks for the help. much appreciated. =)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks