473,466 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to replace sentence?

79 New Member
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
3 3160
nithinpes
410 Recognized Expert Contributor
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
Archanak
79 New Member
@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
410 Recognized Expert Contributor
$_ 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

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

Similar topics

1
by: Thomas Bartkus | last post by:
The meaning of REPLACE INTO is clear to me. IF the new record presents new key values, then it is inserted as a new record. IF the new record has key values that match a pre-existing record, then...
2
by: SamIAm | last post by:
Hi I want to replace the contents between brackets i.e. string text = "this is a sentence (replace me)"' Thanks, S
4
by: Lauren Wilson | last post by:
Hi folks, We have a need to replace sub strings in certain message text. We use the Office Assistant to display help and often use the imbedded formatting commands. Those of you who have used...
1
by: wizardRahl | last post by:
Hello, I'm looking to write a program that can replace words in a string given a user input. I need to replace the input (usually a sentence given by the user) to be less sexist. For ex. If...
7
by: wizardRahl | last post by:
Hello, I'm looking to write a program that will take a user input (a sentence) and make it less sexist. For ex., User input is -> He who laughs last, laughs best. Program output -> She or he who...
7
by: kuzure | last post by:
Dear friends, I want to read a file, for instance, "testing.txt". Then, I need to put it in a text box. Dim stExample As String stExample = there is something on your right side. I need to...
4
Dasty
by: Dasty | last post by:
My problem: My users will write some text into <textarea> area. All I want to do with javascript is to take that textarea.value and replace all url links there with regular <a href> and then put...
6
by: chrispoliquin | last post by:
I am using regular expressions to search a string (always full sentences, maybe more than one sentence) for common abbreviations and remove the periods. I need to break the string into different...
19
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.