Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help - Flat text file pattern matching dilema 1

Status
Not open for further replies.

qjade

Programmer
Jun 7, 2004
42
US
Hello friends,
I recently ran into a problem with my perl prog that reads from a flat text file for patterns and was hoping that one of the many Perl guru here can give me a hand or drop a few lines of suggestions.
My problem is as follow: My prog use to read one line of text at a time from a flat file. It splits the "Before text:" below whenever there are two spaces or more are found(example: $time=10:00 AM, $event=Morning Meeting, $room=Room 100). However, the creator of the file (which I can not control) sometimes play tricks on me and only puts in one space between the "10:00 AM" and the "Morning Meeting". This result in my variables having the wrong data (example: $time=10:00 AM Morning Meeting, $event=Room 100, $room=).

Please show me how to resolve this problem or just any general suggestion how you would do the "split" differently. My line of code is just a simple:
($k, $l, $m) = split(/\ +/,$text);

Thank you in advance.

Before text:
10:00 AM Morning Meeting Room 100
Results:
$time=10:00 AM, $event=Morning Meeting, $room=Room 100
-------------
Problem Text:
10:00 AM Morning Meeting Room 100
Bad Results:
$time=10:00 AM Morning Meeting, $event=Room 100, $room=
 
if the user is having to input 2 spaces, can you not get them to use commas instead and split via the comma or other separator.

or write a routine to capture the data and do some validation before adding it to the text file?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Unfortunately, as with many things in the business world, I have no control over the file. It is what it is and I have to live with it.
 
Can you be sure there is always AM/PM on the end of the date and the room bit will always start 'Room'.

That might at least give you something to gauge start and end of each section.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I can guarantee that the Time element will alway end with AM or PM. However, the Room element may or may not be present. Hope that helps you help me. Thanks for replying 1DMF. Much appreciated.
 
Code:
while(<DATA>) {
  my ($time, $event, $room) = /(.+?M)\s+(.+?)\s{2,}(.+)/;
  print "time= $time, event = $event, room = $room\n";
}
__DATA__
10:00 AM  Morning Meeting         Room 100
10:00 AM Morning Meeting         Room 100
 
I missed a line:

Code:
while(<DATA>) {
  [b]$_ .= '  ';[/b]
  my ($time,$event,$room) = /(.+?M)\s+(.+?)\s{2,}(.+)/;
  print "time=[$time], event=[$event], room=[$room]\n";
}
__DATA__
10:00 AM  Morning Meeting         Room 100
10:00 AM Morning Meeting         Room 100
10:00 AM Morning Meeting

The highlighted line appends blank spaces so that the regex will match for entries without rooms, in which case $room will end up being ' '.
 
Thank you for responding Brigmar.

One additional silly question though - how do I change your suggestion slightly to fit into my program? I have the line of text already save in $text variable - where do I go from there?

Instead of assuming that all lines of text in the file is in the same format...how do I treat just one line at a time? Hope that make some sense. I have it now where a conditional statement catches the line that I need (with Time, Event, and Room) and then wish to resolve/split it according to your suggestion.

Could something along the line following work?
#$text = line of text in question
my ($time,$event,$room) = /(.+?M)\s+(.+?)\s{2,}(.+)/$text;

Thanks a million again.
 
Code:
my ($time,$event,$room) = $text =~ /(.+?M)\s+(.+?)\s{2,}(.+)/;
 
Works perfectly!

Thank you KevinADC and brigmar. Very much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top