Pattern Matching and Extraction

D

doni

Hi,

I am writing a program where based upon an user input, I perform a
pattern match to extract the user input from the file. If there is a
match then I would like to print the 4th and 5th field of the next line
from where the pattern match occured. Can anyone let me know how can I
perform this.

Here is how the data is represented in the file.

Name
-> Address1 Address2 City ZipCode
Name
-> Address1 Address2 City ZipCode
Name
-> Address1 Address2 City ZipCode
Name
-> Address1 Address2 City ZipCode
Name
-> Address1 Address2 City ZipCode

Thanks,
doni
 
J

John Bokma

doni said:
Hi,

I am writing a program where based upon an user input, I perform a
pattern match to extract the user input from the file. If there is a
match then I would like to print the 4th and 5th field of the next line
from where the pattern match occured. Can anyone let me know how can I
perform this.

What have you tried so far.
Here is how the data is represented in the file.

Name
-> Address1 Address2 City ZipCode

Better, post actual data, maybe munged a bit. What's after the ->? A tab,
a fixed number of spaces? Can an address contain spaces? A city? A
zipcode? Are the fields fixed width, etc.
 
D

doni

John,

Here is how the data looks like.

Patrick
-> 2345 CountryLane Houston 77023
Craig
-> 7200 Archstone Apts, #404 San Ramon 94583
Bret
-> 501 TownHouse Apts, #202 Houston 77023
Gilbert
-> 4200 OakView Apts, #6, Nashua 03060

So, if the user inputs "Patrick" I should be able to match Patrick in
the data file and then print "Houston" and "77023".

Thanks,
doni
 
J

John Bokma

doni said:
John,

Here is how the data looks like.

Patrick
-> 2345 CountryLane Houston 77023
Craig
-> 7200 Archstone Apts, #404 San Ramon 94583
Bret
-> 501 TownHouse Apts, #202 Houston 77023
Gilbert
-> 4200 OakView Apts, #6, Nashua 03060

Don't top post (if you don't know what that means, use Google to look it
up).

If you can change your data format I recommend to do so. I can give you a
regexp that matches the 2nd line, but it might fail for data you and I
don't expect at this stage.

Use CSV or XML and a module that can handle data in the format you choose.
 
D

doni

John,
I cannot change the format as the generated file has data in that
format. I will take a look at using CSV or XML for handling my data
format.
If you can point me to any example that uses CSV or XML that handles
these kinds of data formats, it will be really helpful.

Thanks,
doni
 
D

doni

thanks a lot Michele, I was able to get the result I wanted using your
method.

doni.


Michele said:
Here is how the data looks like.

Patrick
-> 2345 CountryLane Houston 77023
Craig
-> 7200 Archstone Apts, #404 San Ramon 94583
Bret
-> 501 TownHouse Apts, #202 Houston 77023
Gilbert
-> 4200 OakView Apts, #6, Nashua 03060

So, if the user inputs "Patrick" I should be able to match Patrick in
the data file and then print "Houston" and "77023".

Something like the following may come close:

while (<$fh>) {
chomp;
next unless $_ eq 'Patrick';
defined($_=<$fh>) or last;
print "@{[ (split)[-2,-1] ]}";
}

Of course in "real" code you would add some check, in particular you
would make sure that the fields got from split() are defined and
consistent with what you expect them to be like. OTOH your "format"
seems inconsistent: it's partly comma separated, partly space
separated, and differently so on different lines. Moreover some city
name seem to include space(s). Thus it's hard to say what one should
really split() on.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
J

John Bokma

doni said:
John,
I cannot change the format as the generated file has data in that
format.

Then you might have a big problem at hand unless tabs are used to separate
the fields.
 
T

Tad McClellan

PLEASE DO NOT TOP-POST!

If you do not know what "top post" means, then you should try to find
out as soon as possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top