Solved

String preprocessing

Posted on 2002-04-11
1
136 Views
Last Modified: 2010-03-05
Hi,

I'm trying to write a perl script to do the followings:

1. Calculate the number of words from a string.
2. Check the last word exist in a text file. If exist    move it to the beginning of the string.
3. Extract the first two word of the string.
4. Find the occurence of the word "d/o" and extract every words before that.

Eg.
   string = "Rajiv Mani d/o Arumugam"
   ...code to count the word length which is 4
   if (last_word "Arumugam" is in file c:\temp\Names.txt)
   {
     code to move last_word to first_word
     string = "Arumugam Rajiv Mani d/o"
     if (string has "d/o")
      code to extract "Arumugam Rajiv Mani"
     else
      code to retrieve "Arumugam Rajiv"

Thanks,
Nick

     




0
Comment
Question by:n_chai
1 Comment
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 6936041
$string ||= "Rajiv Mani d/o Arumugam";
$count = () = $string=~/(\S+)/g ;
$last_word = $1;
open F,'<c:/temp/Names.txt' or die "Can't open c:/temp/Names.txt $!";
if( grep/\b\Q$last_word\E\b/i,<F> ){
    $string =~ s/(.*)(\s+)(\S+)/$3$2$1/s;
    if( $string =~ /(.*)\bd\/o\b/ ){
        $extract = $1;
    }else{
        ($retrieve) = $string =~ /(\S+\s+\S+)/;
    }
}
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now