Hello to the monastery!

First, in the for loop of the second block I think you should use $var2 instead of $var in the if condition. The Perl compiler told me this after I inserted

use strict; use warnings;
at the beginning, which is recommended.

Second, the g modifier in the second block

if ($rows[2] =~ m/\[R\]/g) {
skips the first [R] of the second column. So, it is not available any more for the next match.

If I change your script to

use strict; use warnings; open (OUTFILE, "< History21.txt") or die "Could not open source file. +$!"; open (NEW_MED, "> History_MED1.txt") or die "Could not open med file. +$!"; open (NEW_SUR, "> History_SUR1.txt") or die "Could not open sur file. +$!"; my $count2 = 1; while (my $line = <OUTFILE>) { my @rows = split(/\|\|/, $line); if ($rows[1] =~ m/\[R\]/g) { if ((my @var)= $rows[1] =~ m/\](.*?)\[/g) { foreach my $var (@var) { if ($var =~ /^\s$/g) { #DO NOTHING SINCE WE ONLY WANT IF THERE IS ANYTHI +NG INSIDE THE 2 R BRACKETS } else { print NEW_MED "$rows[0] || $var\n"; } } } } if ($rows[2] =~ m/\[R\]/) { if ((my @var2)= $rows[2] =~ m/\](.*?)\[/g) { # I AM ASSUMING +IT WILL RETURN SIMILAR TO THE PREVIOUS IF STATEMENT FOR ROW1 ABOVE foreach my $var2 (@var2) { if ($var2 =~ /^\s$/g) { #DO NOTHING SINCE WE ONLY WANT IF THERE IS ANYTHI +NG INSIDE THE 2 R BRACKETS } else { print NEW_SUR "$rows[0] || $var2\n"; } } } } $count2++; } close(NEW_MED); close(NEW_SUR);
I get the missing output.
ZZZZZ00DFL || No pertinent past medical history ZZZZZ00DFL || No pertinent past medical history ZZZZZ00C4H || High Blood Pressure ZZZZZ00C4H || Kidney stones ZZZZZ00C4H || SOAPP-R Score : Moderate/High Risk: 19 ZZZZZ00C4H || Sleep Apnea Assessment(STOP-BANG): ( 2/4 ); 7/2/2014 ZZZZZ00C4H || Orthotic brace tried for pain relief: Yes: limited bene +fit ZZZZZ00C4H || TENS Unit tried for pain relief: Yes: limited benefit ZZZZZ00C4H || Tried Topical Compound Cream?: Yes: limited benefit ZZZZZ00C4H || Acupuncture therapy tried for pain relief: Yes: limited + benefit ZZZZZ00C4H || Chiropractic therapy tried for pain relief: Yes: limite +d benefit ZZZZZ00C4H || Physical therapy tried for pain relief: Yes: limited be +nefit ZZZZZ00C4H || Massage therapy tried for pain relief: Yes: limited ben +efit \ ZZZZZ00C4H || \
and
ZZZZZ00DFL || UNREMARKABLE ZZZZZ00DFL || UNREMARKABLE ZZZZZ00C4H || Hysterectomy ZZZZZ00C4H || Spinal Fusion: L4-5, L5-S!; 2010 \ ZZZZZ00C4H || \

In reply to Re: Extracting multiple match with regex by hexcoder
in thread Extracting matches using boundries with regex by NewMonk2Perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.