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

Is there anyway to use the first REGEX match to generate the second REGEX statement?

Yes, there is, and I see you're already doing this in your regular expression /($pkt) (\w+) = (\d+)/. If, for example, $pkt is "abc.def", the pattern will be the equivalent of /(abc.def) (\w+) = (\d+)/. If you don't want the dot and any other characters to be interpreted as special regex characters, you can write your regex as /(\Q$pkt\E) (\w+) = (\d+)/, and the previous example regex becomes /(abc\.def) (\w+) = (\d+)/ (see quotemeta).

If this isn't working, after a first glance I will venture a guess as to why: your inner while loop is matching against $line_in instead of the current line from DAT_FILE, is that correct? If you want to match against the current line from DAT_FILE, it's enough to change that line to if (/($pkt) (\w+) = (\d+)/) {

If that's not the problem, it would help if you could provide more information on your question: some sample input, the desired program output, any error messages you might be getting, and a description of what the program is supposed to be doing and how that's different from what it's actually doing.

As you said there are other ways your code could be cleaned up (for example, it looks like you're opening and closing the file "$2_tlmval.txt" as OUT_FILE twice in a row, when you could just keep the file open), but first things first :-)

Replies are listed 'Best First'.
Re^2: How to use REGEX Match Variable from one 'While' String in Another 'While' String
by GoldfishOfDoom (Initiate) on May 07, 2014 at 18:40 UTC

    Ah! Much easier than I was expecting. Thank you very much for the help! Seems like removing the $line_in did the trick. The regex match only had alphanumeric characters and underscores, so special characters wasn't an issue.

    I also removed the extra open and close of the file. Thank you for the suggestion! I really appreciate you taking the time to respond.

      The regex match only had alphanumeric characters and underscores...

      If your $pkt_file doesn't contain regular expressions, it's probably better to use \Q$pkt\E, because that'll prevent your program from doing unexpected things if $pkt does someday for whatever reason contain special characters. Similar to what AnomalousMonk wrote below about $1 $2 $3 etc. it's a "future-proofing" measure.