Dear PerlMonks,

I am completely new (apologies in advance for any politically incorrect terminology) to PERL and am trying to write a script that searches a file for a pattern then generates a new file based on a specific match from each pattern.

Using the match from the first file, I need to search a second file for a pattern that contains that match; however, since there are two files and two while statements, the variable that I generated using the match in the first while statement must stay in its own loop. Is there anyway to use the first REGEX match to generate the second REGEX statement?

Here's a sample of the code. I'm sure there are a ton of ways to clean this up, but at the moment, my biggest concern is finding a way to use the match from the first while's REGEX in the second while and REGEX. Any help is greatly appreciated. I've only been working with PERL for two days (with no previous programming experience) so this is all relatively new to me.

use warnings; use strict; # Variables my $pkt_file = $ARGV[0]; my $dat_file = $ARGV[1]; # Read data file open (IN_FILE, "<", $pkt_file) or die "ERROR Opening $pkt_file (Error += $!)\n"; print "Opening: $pkt_file for read...\n"; # Search IN_FILE for Packet Headers while (<IN_FILE>) { my $line_in = $_; if (($line_in =~ /(\w+) (\w+)/)) { open (OUT_FILE, ">>", "$2_tlmval.txt") or die "ERROR Opening + file (ERROR = $!)\n"; print "Opening $2_tlmval.txt\n"; print OUT_FILE " \n"; print OUT_FILE "$2:\n"; #prints CSTOL Label print OUT_FILE " \n"; my $pkt = "$2"; my $out_file = "$2_tlmval.txt"; # Close OUT_FILE close(OUT_FILE); print "Closing: OUT_FILE\n"; #Open data_list file for read open (DAT_FILE, "<", $dat_file) or die "ERROR Opening $dat_f +ile (Error = $!)\n"; print "Opening: $dat_file for read...\n"; # Reopen OUT_FILE for write open (OUT_FILE,, ">>", $out_file) or die "ERROR Opening $out +_file (Error = $!)\n"; print "Re-opening: $out_file for write...\n"; #Search DATA_LIST file while (<DAT_FILE>) { if (($line_in =~ /($pkt) (\w+) = (\d+)/)) { print OUT_FILE "check $pkt $2 vs $3\n"; } } } # Close IN_FILE close(IN_FILE); print "Closing: IN_FILE\n"; #Close OUT_FILE close (OUT_FILE); print "Closing: OUT_FILE\n"; # Close DAT_FILE close(DAT_FILE); print "Closing: DAT_FILE\n";

In reply to How to use REGEX Match Variable from one 'While' String in Another 'While' String by GoldfishOfDoom

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.