Lost Password?


Go Back   CodeCall Programming Forum > Web Development Forum > Perl

Perl Discussion for the PERL language - Practical Extraction and Reporting Language, is a programming language often used for creating CGI programs.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-03-2007, 01:44 AM
lichy lichy is offline
Newbie
 
Join Date: Jun 2007
Posts: 17
Rep Power: 6
lichy is on a distinguished road
Default need help

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-03-2007, 02:57 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,635
Last Blog:
CherryPy(thon)
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

You can use the function substr.
Take a look at this.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-03-2007, 03:29 AM
KevinADC KevinADC is offline
Learning Programmer
 
Join Date: Jan 2007
Posts: 91
Rep Power: 7
KevinADC is on a distinguished road
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-03-2007, 09:40 PM
lichy lichy is offline
Newbie
 
Join Date: Jun 2007
Posts: 17
Rep Power: 6
lichy is on a distinguished road
Default

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-04-2007, 03:52 AM
KevinADC KevinADC is offline
Learning Programmer
 
Join Date: Jan 2007
Posts: 91
Rep Power: 7
KevinADC is on a distinguished road
Default

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;
another way:

Code:
my $string = "HCHATCABCKDLOTSHCNAGWABCHSTABCADLOS";
$string =~ s/(ABC)/($1)/g;
print $string;
next time post some perl code you have tried.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-04-2007, 10:22 PM
lichy lichy is offline
Newbie
 
Join Date: Jun 2007
Posts: 17
Rep Power: 6
lichy is on a distinguished road
Default

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 10:25 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-04-2007, 10:48 PM
KevinADC KevinADC is offline
Learning Programmer
 
Join Date: Jan 2007
Posts: 91
Rep Power: 7
KevinADC is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-04-2007, 10:56 PM
lichy lichy is offline
Newbie
 
Join Date: Jun 2007
Posts: 17
Rep Power: 6
lichy is on a distinguished road
Default

Quote:
Originally Posted by KevinADC View Post
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 11:00 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-05-2007, 01:55 AM
KevinADC KevinADC is offline
Learning Programmer
 
Join Date: Jan 2007
Posts: 91
Rep Power: 7
KevinADC is on a distinguished road
Default

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);
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;
while ($pos < $x) {
   $pos = index $string,$substring,$index;
   print "$substring is at: $pos\n";
   $index = $pos+$n;
};
And there are a number of other ways to code the solution to your (homework?) question.
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-05-2007, 05:05 AM
lichy lichy is offline
Newbie
 
Join Date: Jun 2007
Posts: 17
Rep Power: 6
lichy is on a distinguished road
Default

once again, thanks for the help. much appreciated. =)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 05:31 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 98%

Ads