[EMAIL PROTECTED] wrote:
Hello,
Hello,
I'm having a problem with a regular expression in some code that I inherited. I'm reading from a file and looking for certain parameters (mrn, enc, date, report_subtitle). The file looks like this: %%start %mrn%02333699 %fac%A %gpi% %enc%9720100004 %date%07/07/2007^14:29 %report_type%amit %report_subtitle%OCR (nut %phys_id1%GLENN %phys_id2%RAJPAL %%Pages: 5 %!PS-Adobe %%Title: (orderNotes_1) %%CreationDate: 09/11/07 14:29 %%For: System Man PRINGLE, GLENN O showpage %%PageTrailer end %%Trailer %%thisWasThePreviousStart %mrn%02333699 %enc%0720100004 %date%09/30/2007^23:59 %report_type%PCM %report_subtitle%Order Confirmation Report (nut %phys_id1%PRINGLE My problem is that the regular expression is only displaying the first item (mrn) but it won't display the others (enc, date, report_subtitle)
You have a single pattern so it should display all of them or none of them.
My code is on the following line #!/usr/bin/perl
use warnings; use strict;
#open(FILE, "3688001.ps"); open(FILE, "1234567.txt");
You should *always* verify that the file opened correctly: open FILE, '<', '1234567.txt' or die "Cannot open '1234567.txt' $!";
while ($buf = <FILE>) {
while ( my $buf = <FILE> ) { This reads a single line into the $buf variable.
#print "$buf\n"; if ($buf =~ m/\%mrn\%(\d+).+ \%enc\%(\d+).+ \%date\% (\d{1,2})\/ (\d{1,2})\/ (\d{1,2})\^.+ ^\%report_subtitle\%(.+)\( /xms) {
Your regular expression is trying to match multiple lines but there is only one line in $buf.
##################################################################### ## debug messages for matches ##################################################################### print "HERE --> 1=<$1>\n" if (defined($1)); print "HERE --> 2=<$2>\n" if (defined($2)); print "HERE --> 3=<$3>\n" if (defined($3)); print "HERE --> 4=<$4>\n" if (defined($4)); print "HERE --> 5=<$5>\n" if (defined($5)); print "HERE --> 6=<$6>\n" if (defined($6)); print "HERE --> 7=<$7>\n" if (defined($7));
You are using the numeric variables inside an if conditional and all the captures match one or more characters so the defined test is superfluous.
} } 1;
The "last statement evaluates as true" rule is only required for modules.
I would appreciate if anybody can tell me what I'm doing wrong or missing.
#!/usr/bin/perl use warnings; use strict; #open FILE, '<', '3688001.ps' or die "Cannot open '3688001.ps' $!"; open FILE, '<', '1234567.txt' or die "Cannot open '1234567.txt' $!"; while ( my $buf = <FILE> ) { #print "$buf\n"; print <<TEXT if $buf =~ /%mrn%(\d+)/; HERE --> 1=<$1> TEXT print <<TEXT if $buf =~ /%enc%(\d+)/; HERE --> 2=<$1> TEXT print <<TEXT if $buf =~ /%date%(\d+)\/(\d+)\/(\d+)/; HERE --> 3=<$1> HERE --> 4=<$2> HERE --> 5=<$3> TEXT print <<TEXT if $buf =~ /%report_subtitle%(.+)\(/; HERE --> 6=<$1> TEXT } __END__ John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/