|
||||||
| Perl Discussion for the PERL language - Practical Extraction and Reporting Language, is a programming language often used for creating CGI programs. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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? |
| Sponsored Links |
|
|
|
|||||
|
You can use the function substr.
Take a look at this.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. - To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. - To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. I'm always up for a chat, so feel free to contact me... |
|
|||
|
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: Code:
my $string = "TTTTTTTTTTTTTTTTTTTABCTTTTTTTABCTTTTTTT"; $string =~ s/([^T]+)/($1)/g; print $string; Code:
my $string = "HCHATCABCKDLOTSHCNAGWABCHSTABCADLOS"; $string =~ s/(ABC)/($1)/g; print $string; ![]() |
| Sponsored Links |
|
|
|
|||
|
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 09:25 PM. |
|
|||
|
Quote:
$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 10: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:
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);
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. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| Xav | ........ | 1024.41 |
| MeTh0Dz|Reb0rn | ........ | 974.08 |
| morefood2001 | ........ | 850.04 |
| John | ........ | 841.93 |
| WingedPanther | ........ | 661.52 |
| marwex89 | ........ | 575.59 |
| Brandon W | ........ | 456.18 |
| chili5 | ........ | 292.12 |
| orjan | ........ | 187.41 |
| Steve.L | ........ | 181.88 |
Goal: 100,000 Posts
Complete: 79%