jlope043 has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I am trying to copy my original file but add a space after my $find. I have ran the code but nothing seems to export and I can't figure out why. I have used this format before on my of my other scripts but this one I can't get to function properly.

use strict; for $find = "END OF EXPANDED DATA"; open (NEW, ">", "FILE_OUTPUT.txt" ) or die "could not open:$!"; open (FILE, "<", "FILE.txt") or die "could not open:$!"; while (<FILE>) { if (/$find/){ print NEW "\n"; } } close (FILE); close (NEW);

INPUT FILE:

LINE 1 LINE 2 LINE 3 END OF EXPANDED DATA LINE 4 LINE 5 LINE 6 LINE 7 END OF EXPANDED DATA LINE 8 LINE 9

OUTPUT FILE:

LINE 1 LINE 2 LINE 3 END OF EXPANDED DATA LINE 4 LINE 5 LINE 6 LINE 7 END OF EXPANDED DATA LINE 8 LINE 9

Replies are listed 'Best First'.
Re: Add Space after $Find
by choroba (Cardinal) on Jul 05, 2017 at 19:27 UTC
    Your code doesn't compile.
    Global symbol "$find" requires explicit package name at ./1.pl line 4. syntax error at ./1.pl line 4, near "$find =" Global symbol "$find" requires explicit package name at ./1.pl line 10 +. Execution of ./1.pl aborted due to compilation errors.

    After fixing the problems (and indentation and whatever else is wrong), also add

    print NEW $_;

    to copy the contents of FILE into NEW.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      That was the problem! Thank you. Removed the $find and just put the simple text I was searching for. Correction below.

      use strict; open (NEW, ">", "FILE_OUTPUT.txt" ) or die "could not open:$!"; open (FILE, "<", "UBTEST.txt") or die "could not open:$!"; while (<FILE>) { print NEW $_; if (/END OF EXPANDED DATA 5010/){ print NEW "\n"; } } close (FILE); close (NEW);
Re: Add Space after $Find
by thanos1983 (Parson) on Jul 06, 2017 at 08:58 UTC

    Hello jlope043,

    I am glad that you resolved your problem. I just wanted to add a minor comment here that maybe it will help you in future.

    Based on you code that you provided us, I noticed a few things. Always always!!!!!!! use strict and use warnings short documentation on why Use strict and warnings. Also do not use bare words for file handles, read here why Don't Open Files in the old way. Every time you open a file handle or close one use die or warn.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!