in reply to Edit a New file in place after reading it in

First uncomment the use warning and change 'warning' to 'warnings'. Your intent to use strictures (strict and warnings) is good, but ignoring errors is really bad.

Now consider the following self contained example script:

use strict; use warnings; my $template = <<FILE; --------------------------------------- Basic Information: --------------------------------------- Build Report Rev: 02 Builder: Xi Wong Part Name: MG5237ALL5X Customer Name: SI Route: S5H2-12A FILE my $outText; my %subs = ( 'Build Report Rev' => '01', 'Builder' => 'Lucca P.', 'Part Name' => 'MG5415DP', 'Customer Name' => 'SA', 'Route' => 'S5H2-12A' ); # Info to New Buld Report open my $base_fh, '<', \$template; open my $New_fh, '>', \$outText; while (my $line = <$base_fh>) { if ($line =~ /^([^:]+):(\s+)/ && exists $subs{$1}) { $line = "$1:$2$subs{$1}\n"; } print $New_fh $line; } close $base_fh; close $New_fh; print $outText;

Prints:

--------------------------------------- Basic Information: --------------------------------------- Build Report Rev: 01 Builder: Lucca P. Part Name: MG5415DP Customer Name: SA Route: S5H2-12A

which I think does what you want. Notice the hash used to store the substitution values. In a real life example that could be populated from a database or from a file containing the required edits. Although, in real life it feels like this whole exercise should be done using a database. You may be interested in Databases made easy for an introduction to using databases.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^2: Edit a New file in place after reading it in
by perlynewby (Scribe) on Dec 22, 2021 at 15:54 UTC

    Grandfather, I appreciate the help on going about substituting the old values for the new values. I didn't figure out I could use a hash. I was having a blockage. Your code captured the task I wanted to get done. I now have to do this for 100's of records. I will also read your tutorial on databases (SQLlite) before attempting to clean up my code and show the updates here. I may have more questions coming when using the SQL database to write up my code. Thanks, Perlynewby

Re^2: Edit a New file in place after reading it in
by perlynewby (Scribe) on Dec 29, 2021 at 02:24 UTC
    Re^2: Edit a New file in place after reading it in
    by perlynewby (Scribe) on Dec 28, 2021 at 19:50 UTC

      I am having a bit of doubt about deciphering the regex; so, here's my best shot. Please correct me when I am wrong

       $line =~ /^([^:]+):(\s+)/ && exists $subs{$1}

      So, regex on $line is as follows (according to me ;-) )

      "^" regex string must be at the start of the line.

      The string is grouped by using the "( )", if the entire string in the "()" matches then it will substitute with a new value in the hash.

      I am not sure why the class  "[ ]" are opened or mean in this regex...???

      Whatever is in the class bracket, it is looking for a match of"^:" character. I am not sure about the carrot "^" does here before the ":" ?? then the class bracket closes.

      The "+" plus signs indicates that there may be more strings that match the ":" character ...maybe? I am guessing here. then we close the group.

      Then the character ":" appears again after the parenthesis has closed. I'm not sure what this ":" does here.

      then (s+) there may be one or more spaces after the first regex match.

      Then checks if that regex it just performed exists, if it does, then the new hash value replaces old to new value.

      I could not run your code because it kept giving me an error

      Error

      reading file No such file or directory

      Any ideas why this "reading file does not exist" when it was integrated in the code