Solved

String preprocessing

Posted on 2002-04-11
8
145 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
  • 4
  • 3
8 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 6936030
$string = "Rajiv Mani d/o Arumugam";
$count = () = $string=~/(\S+)/g ;
$last_word = $1;
open F,'<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
 

Author Comment

by:n_chai
ID: 6936240
Hi User Ozo,

they seems to be some problem when my names has more than 4 words. eg "Abdul Aziz d/o Mohamad Yusoff"

the system will return me "Abdul Aziz  Mohamad" Any idea ?

Nick
0
 

Author Comment

by:n_chai
ID: 6936241
Hi User Ozo,

they seems to be some problem when my names has more than 4 words. eg "Abdul Aziz d/o Mohamad Yusoff"

the system will return me "Abdul Aziz  Mohamad" Any idea ?

Nick
0
 

Author Comment

by:n_chai
ID: 6936263
Hi User Ozo,

they seems to be some problem when my names has more than 4 words. eg "Abdul Aziz d/o Mohamad Yusoff"

the system will return me "Abdul Aziz  Mohamad" Any idea ?

Nick
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 6936344
When I run it with
$string = "Abdul Aziz d/o Mohamad Yusoff";m
"Yusoff Abdul Aziz "
0
 
LVL 84

Expert Comment

by:ozo
ID: 6936349
When I run it with
$string = "Abdul Aziz d/o Mohamad Yusoff";
I get
"Yusoff Abdul Aziz " 
in $extract
0
 

Author Comment

by:n_chai
ID: 6936434
Hi Ozo,

I think I made a mistake in my requirements. Actually what I need to do is whenever the system detected "d/o" I just need the words before it. eg. "Abdul Aziz d/o Mohamad Yusoff" I need "Abdul Aziz" only.

Sorry.
0
 
LVL 10

Expert Comment

by:oleber
ID: 6936718
my $string = "Abdul Aziz d/o Mohamad Yusoff";
$string =~ m/^(.*)d\/o/;
print "$1\n";

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl - Mawk 2 86
perl script to search for files containing text 3 72
How to generate square thumbnail using perl 13 77
html form to write data to csv 24 94
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

947 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

22 Experts available now in Live!

Get 1:1 Help Now