CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Unexpected Output

 



jared.collier
Novice

Jul 1, 2009, 10:41 AM

Post #1 of 3 (2333 views)
Unexpected Output Can't Post

I'm having trouble extracting information from a series of documents. The snippet of code I've included is meant to take the input file and store the different sections in an array (although the code to store in the array is not yet present, instead I've inserted a test to find out what $1 returns). I'm calling my program with MRN as a parameter.

open(CURRENT_FILE, $currentFile) || die ("Could not open the input file $currentFile - $!");

if ($ARGV[0] eq 'MRN' ||
$ARGV[0] eq 'DOB' ||
$ARGV[0] eq 'Gender' ||
$ARGV[0] eq 'SSN' ||
$ARGV[0] eq 'Account')
{
while (<CURRENT_FILE>) {
if ($_ =~ /(PID.*)$/) {
print "\n$1\n";
}else {
print "\nERROR\n";
}
}
}elsif..........

In this case, I'm trying to pull just the PID section of a file which I'll then split and extract the sub-field information. Below is a scrubbed example of an input file:

MSH|^~\&|TESTADT|HHH|AAA-ADT|myCompany|200003470212||ADT^A08|ACSC-ADT.1.355132|P|2.7
PID|1||M113928|M1111|LASTNAME^FIRSTNAME^E||11112229|M||||||||||V321319293949596960
PV1|1|I|LOCATION^1^A|||||||MED/SURG||||||||IN||||||||||||||||||||||||||200000000011|212312312306
PV2|||||||||||||||||||||||||DIS

in this particular case, the output looks like:

PV1|1|I|LOCATION^1^A|||||||MED/SURG||||||||IN||||||||||||||||||||||||||200000000011|212312312306
PV2|||||||||||||||||||||||||DIS

While I would like to extract:

PID|1||M113928|M1111|LASTNAME^FIRSTNAME^E||11112229|M||||||||||V321319293949596960

Thanks in advance for the insight.

I have a feeling it has something to do with carriage returns and new line special characters (I tried replacing the $ in the regex with \r\n but had no luck). It should be noted that when I open the target document in notepad, the new line characters appear as boxes (almost like []).


KevinR
Veteran


Jul 1, 2009, 2:53 PM

Post #2 of 3 (2330 views)
Re: [jared.collier] Unexpected Output [In reply to] Can't Post

Your code while awkwardly written does seem to work:


Code
while (<DATA>) { 
if (/(PID.*)$/) {
print $1;
}
else {
print "ERROR\n";
}
}

__DATA__
MSH|^~\&|TESTADT|HHH|AAA-ADT|myCompany|200003470212||ADT^A08|ACSC-ADT.1.355132|P|2.7
PID|1||M113928|M1111|LASTNAME^FIRSTNAME^E||11112229|M||||||||||V321319293949596960
PV1|1|I|LOCATION^1^A|||||||MED/SURG||||||||IN||||||||||||||||||||||||||200000000011|212312312306
PV2|||||||||||||||||||||||||DIS


Output:


Code
ERROR 
PID|1||M113928|M1111|LASTNAME^FIRSTNAME^E||11112229|M||||||||||V321319293949596960ERROR
ERROR


What you probably want to do is something like this:


Code
while (<DATA>) { 
if (/^PID\|/) {
chomp;
my @tokens = split(/\|/);
#do somthing with @tokens
}
else {
print "ERROR\n";
}
}

__DATA__
MSH|^~\&|TESTADT|HHH|AAA-ADT|myCompany|200003470212||ADT^A08|ACSC-ADT.1.355132|P|2.7
PID|1||M113928|M1111|LASTNAME^FIRSTNAME^E||11112229|M||||||||||V321319293949596960
PV1|1|I|LOCATION^1^A|||||||MED/SURG||||||||IN||||||||||||||||||||||||||200000000011|212312312306
PV2|||||||||||||||||||||||||DIS

-------------------------------------------------


jared.collier
Novice

Jul 2, 2009, 5:17 AM

Post #3 of 3 (2323 views)
Re: [KevinR] Unexpected Output [In reply to] Can't Post

Late night I solved it in a similar fashion to your post. This morning however, I've replaced the functionality with a series of split statements. Thanks for the reply.

-Jared

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives