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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |