How to replace sentence?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Archanak
    New Member
    • Sep 2006
    • 79

    How to replace sentence?

    Hi,

    I have an array like this:

    Code:
    @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");
    I have to highlight the word "RNA-binding protein" i have done using below statement and the output is in arr1

    Code:
    $str=~s/(\bRNA-binding protein\b)/<span style="background-color:#E1FF77">$1<\/span>/img;
    
    @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");
    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.
    Code:
    foreach(@arr)
    { 
       foreach $sent(@arr1)
      {
           if($_=~/$sent/ig)
          {
            $_=~s/$_/$sent/i;
        }
    }
    print "<br> $_ <br>";
    }
    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
    Last edited by eWish; Nov 26 '08, 11:42 PM. Reason: Fixed code tags
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    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:
    Code:
    foreach $str (@arr) {
     foreach $sent (@arr1) {
       if($str=~/$sent/i) {  #
            $str=$sent; # since you are replacing original with matched
    }
    }
    print "<br> $str <br>";
    }
    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?

    Comment

    • Archanak
      New Member
      • Sep 2006
      • 79

      #3
      Originally posted by nithinpes
      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:
      Code:
      foreach $str (@arr) {
       foreach $sent (@arr1) {
         if($str=~/$sent/i) {  #
              $str=$sent; # since you are replacing original with matched
      }
      }
      print "<br> $str <br>";
      }
      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?
      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.
      Code:
      @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
      
      @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
      
      @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;
      I am comparing @arr and @arr1 if its matching substitue @arr1 with @arr2 like this:

      Code:
      foreach (@arr)
      {
          foreach $sent(@arr1)
         {
             if($_=~/$sent/ig)
            {
                foreach $sent2(@arr2)
                {
                   $_=~s/$_/$sent2/ig;# itried wit this also
                  $_=$sent2#i tried with this also
                 }
            }
           print "<br> ** $_ <br>";
      }
      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):
      Code:
      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
      How do i substitute sentence?

      any suggestions?

      with regards
      Archana

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        $_ would lose it's scope/value inside the inner loops. Use a variable in the first foreach loop.
        Code:
        foreach $first (@array) {

        Comment

        Working...