in reply to Removing white space from the file

While there's lots of good information in the previous answers about how to go about doing this, I tried to modify your script as little as possible.

Notes follow, as the changes needed were fairly extensive.

#!/usr/bin/perl use strict; use warnings; # Set aside a place to store the data between read and write phases my @OutputBuffer = (); # Read whole file into memory, since we will eventually overwrite it # Often called "slurp" open(my $inputFH, "<", "uuu.txt") || die "cant open the original file" +; while (my $inputLine = <$inputFH>) { print $inputLine; # Since we're here, let's process the data # Often called "digest" chomp $inputLine; $inputLine =~ s/\s//g; push @OutputBuffer, $inputLine; } close $inputFH; # Destroy old file and rewrite with new data # Often called "spew" open(my $outputFH, ">", "uuu.txt") || die "cant open the new file"; foreach my $outputLine (@OutputBuffer) { print $outputFH "$outputLine\n"; } close $outputFH; # Fini exit;

This adjusts your script to remove the whitespace, but has not yet added code to remove the numerical characters. I think you can probably handle that on your own, based on the code you've already generated.

Notes

  1. If you wish to read in a file and then overwrite it with the modified data, the data has to live someplace in the interim.
    • You can't It's so convoluted to try to read and write a text file in place as to be roughly equivalent to simply saying you can't do it.
    • You further specified that you did not wish to write another file in the interim
    • Thus, memory is the logical remaining option.
  2. Reading the whole file into memory, processing it, and then dumping it all out to disk, is often referred to as "slurp, digest, and spew".
    • This works fine on small files. Yours seems to be a small file.
    • This works less and less well as the file size gets larger. Often referred to as "does not scale well".
  3. So, the changes to your script I made above:

Good luck!