By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,675 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

How to replace sentence?

P: 79
Hi,

I have an array like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. @arr=("TDP-43 is a highly conserved, 43-kDa RNA-binding protein implicated to play a role in transcription repression, nuclear organization, and alternative splicing","More recently, this factor has been identified as the major disease protein of several neurodegenerative diseases","Our data further evidence TDP-43 as a multifunctional RNA-binding protein for a diverse set of cellular activities");
  3.  
I have to highlight the word "RNA-binding protein" i have done using below statement and the output is in arr1

Expand|Select|Wrap|Line Numbers
  1.  
  2. $str=~s/(\bRNA-binding protein\b)/<span style="background-color:#E1FF77">$1<\/span>/img;
  3.  
  4. @arr1=("TDP-43 is a highly conserved, 43-kDa RNA-binding protein implicated to play a role in transcription repression, nuclear organization, and alternative splicing","Our data further evidence TDP-43 as a multifunctional RNA-binding protein for a diverse set of cellular activities");
  5.  
I want to match the sentences present in arr1 and arr2 if both are matching the arr2 should replace arr1 and rest print the contents.

Here is the code.
Expand|Select|Wrap|Line Numbers
  1. foreach(@arr)
  2.    foreach $sent(@arr1)
  3.   {
  4.        if($_=~/$sent/ig)
  5.       {
  6.         $_=~s/$_/$sent/i;
  7.     }
  8. }
  9. print "<br> $_ <br>";
  10. }
  11.  
If the sentences are matching the highlighted sentence should be substituted in place of original sentence and i have to print the rest.

I tried like this but the last string is getting replaced and its not giving proper results.

How can i substitute the matched sentence with the original sentence and print the sentences??

Any suggestions?

with regards
Archana
Nov 26 '08 #1
Share this Question
Share on Google+
3 Replies


nithinpes
Expert 100+
P: 410
If I understood your problem correctly, you are trying to compare sentences in two arrays and replace the original sentence with matched ones once you find a match. Try this:
Expand|Select|Wrap|Line Numbers
  1. foreach $str (@arr) {
  2.  foreach $sent (@arr1) {
  3.    if($str=~/$sent/i) {  #
  4.         $str=$sent; # since you are replacing original with matched
  5. }
  6. }
  7. print "<br> $str <br>";
  8. }
  9.  
In your descriprion, Both @arr and @arr1 have the same elements. Please be clear on what you need if this is not what you are looking for.
Are you trying to replace only the desired portion of sentence with highlighted ones?
Nov 27 '08 #2

P: 79
@nithinpes
Hi,

what u said is actually right!!!

Actually one is the main array and other is an array with highlighted terms.

i want to compare them and print the same.

Here is other example.
Expand|Select|Wrap|Line Numbers
  1. @arr=("TDP-43 is a highly conserved, 43-kDa RNA-binding protein implicated to play a role in transcription repression, nuclear organization, and alternative splicing","For the splicing activity, the factor has been shown to be mainly an exon-skipping promoter","Our data further evidence TDP-43 as a multifunctional RNA-binding protein for a diverse set of cellular activities.");#main array
  2.  
  3. @arr1=("TDP-43 is a highly conserved, 43-kDa RNA-binding protein implicated to play a role in transcription repression, nuclear organization, and alternative splicing","Our data further evidence TDP-43 as a multifunctional RNA-binding protein for a diverse set of cellular activities");#array to be matched
  4.  
  5. @arr2=("TDP-43 is a highly conserved, 43-kDa RNA-binding protein implicated to play a role in transcription repression, nuclear organization, and alternative splicing","Our data further evidence TDP-43 as a multifunctional RNA-binding protein for a diverse set of cellular activities")#~s/(\bRNA-binding protein\b)/<span style="background-color:#E1FF77">$1<\/span>/img;
  6.  
  7.  
I am comparing @arr and @arr1 if its matching substitue @arr1 with @arr2 like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. foreach (@arr)
  3. {
  4.     foreach $sent(@arr1)
  5.    {
  6.        if($_=~/$sent/ig)
  7.       {
  8.           foreach $sent2(@arr2)
  9.           {
  10.              $_=~s/$_/$sent2/ig;# itried wit this also
  11.             $_=$sent2#i tried with this also
  12.            }
  13.       }
  14.      print "<br> ** $_ <br>";
  15. }
  16.  
The output obtained was like this rna binding protein highlighted:
[/code]
Our data further evidence TDP-43 as a multifunctional RNA-binding protein for a diverse set of cellular activities
For the splicing activity, the factor has been shown to be mainly an exon-skipping promoter
Our data further evidence TDP-43 as a multifunctional RNA-binding protein for a diverse set of cellular activities
[/code]

The first matched sentence is getting replaced and its not getting printed at all!!!
The last matched sentence is getting replaced with the first matched sentence.

Actually the output should be like this with RNA binding protein should be highlighted( highlighted sentences):
Expand|Select|Wrap|Line Numbers
  1. TDP-43 is a highly conserved, 43-kDa RNA-binding protein implicated to play a role in transcription repression, nuclear organization, and alternative splicing 
  2. For the splicing activity, the factor has been shown to be mainly an exon-skipping promoter
  3. Our data further evidence TDP-43 as a multifunctional RNA-binding protein for a diverse set of cellular activities
  4.  
How do i substitute sentence?

any suggestions?

with regards
Archana
Nov 27 '08 #3

nithinpes
Expert 100+
P: 410
$_ would lose it's scope/value inside the inner loops. Use a variable in the first foreach loop.
Expand|Select|Wrap|Line Numbers
  1. foreach $first (@array) {
  2.  
Nov 28 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.