Good Morning Perl Gurus, I have been trying to figure out all morning but can not understand why the 2nd set of if statements for $rows2 does not capture all data within the R brackets like I intended. Any help would be greatly appreciated! Below is what the file for OUTFILE file handler looks like:

Patient_ID || Med_Hist || Sur_Hist ZZZZZ00DFL || [R] [R]No pertinent past medical history [R] [R] || + [R]UNREMARKABLE [R] [R] ZZZZZ00DFL || [R] [R]No pertinent past medical history [R] [R] || + [R]UNREMARKABLE [R] [R] [R] ZZZZZ00C4H || || ZZZZZ00C4H || [R] [R]High Blood Pressure [R]Kidney stones [R] [R]S +OAPP-R Score : Moderate/High Risk: 19 [R] [R]Sleep Apnea Assessment +(STOP-BANG): ( 2/4 ); 7/2/2014 [R] [R]Orthotic brace tried for pain + relief: Yes: limited benefit [R]TENS Unit tried for pain relief: Ye +s: limited benefit [R]Tried Topical Compound Cream?: Yes: limited be +nefit [R] [R]Acupuncture therapy tried for pain relief: Yes: limited + benefit [R]Chiropractic therapy tried for pain relief: Yes: limited + benefit [R] [R]Physical therapy tried for pain relief: Yes: limited + benefit [R]Massage therapy tried for pain relief: Yes: limited bene +fit \[R\] \[R\] || [R] [R]Hysterectomy [R]Spinal Fusion: L4-5, L5 +-S!; 2010 \[R\] \[R\]

Here is the code:

open (OUTFILE, "< C:\\Scripts\\TEST\\History21.txt") or die "Could not + open source file. $!"; open (NEW_MED, "> C:\\Scripts\\TEST\\History_MED1.txt") or die "Could + not open source file. $!"; open (NEW_SUR, "> C:\\Scripts\\TEST\\History_SUR1.txt") or die "Could + not open source file. $!"; $count2 = 1; while ($line = <OUTFILE>) { @rows = split(/\|\|/, $line); if ($rows[1] =~ m/\[R\]/g) { if ((@var)= $rows[1] =~ m/\](.*?)\[/g) { foreach $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\]/g) { if ((@var2)= $rows[2] =~ m/\](.*?)\[/g) { # I AM ASSUMING IT +WILL RETURN SIMILAR TO THE PREVIOUS IF STATEMENT FOR ROW1 ABOVE foreach $var2 (@var2) { if ($var =~ /^\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);

The code brings back most of what I want, but it excluded the data from lines 2 and 3 of the file(line 1 is headers) which has "UNREMARKABLE" for "Sur_Hist" column, I need ALL data within the 2 brackets

[R]....[R]

captured unless its just a space like below then I need it excluded

[R].[R]

In reply to 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.