Jump to content

Perl Script to Remove Strings


Recommended Posts

I started working on a Perl program to remove strings from a BoA script (the idea is to do a spell check on the strings, then use a diffent program to merge the corrected strings back in again). It's been a few months since I've used Perl, and I was never formally taught how to write data to a file. So predictably, I've gotten errors I don't know how to solve.

 

I've tried explicitly setting a variable to the result of <IN> instead of relying on $_, to no luck. After trying several different ways of coding it and inserting debug print statements, I get the feeling that the problem arises with:

Code:
print OUT "$1\n";
When I removed OUT (getting print to go to STDOUT), I got:
Code:
Use of uninitialized value in concatenation (.) or string
Anyways, any help would be appreciated; Word doesn't catch all the errors when the strings are surrounded by code.

 

--------------------

Code:
while (@ARGV) {	my $file = shift @ARGV;		open(IN,$file) ||		die "Could not open '$file': $!";		open(OUT,">C-$file") ||		die "Could not create 'C-$file': $!";		while (<IN>) {		while (/".*"/) {			s/$[^"]*"([^"]*)"//;			print OUT "$1\n";		}	}		close(IN) ||		die "Could not close '$file': $!";		close(OUT) ||		die "Could not close 'C-$file': $!";}
Link to comment
Share on other sites

Couldn't Google astring either. Even if I could find it, I'd still like to know what I was doing wrong. This should be a relatively easy task.

 

--------------------

IF I EVER BECOME AN EVIL OVERLORD:

I will hire one hopelessly stupid and incompetent lieutenant, but make sure that he is full of misinformation when I send him to capture the hero.

Link to comment
Share on other sites

Bump.

 

After reading the RegEx chapter over and over to find out why I couldn't capture the string under $1, I remembered that $ signifies the end of the pattern, not the beginning. Oops.

 

Anyways, here's the two programs. The intent is to do some spell-checking in between. After extensive testing (cough), the programs appear to function perfectly. Seeing the immense amount of attention this idea has received, I might come back to these programs later and make it possible for use outside of command line usage. tongue

 

--------------------

Code:
use warnings;use strict;=head1 'precheck' by Dintiradan - Version 0.1Last updated July 28, 2006Contact me with any suggestions or problems at dintiradan(at)yahoo(dot)comFeel free to use and modify this program for your scripts, provided:Original documentation is preserved (unless it no longer applies),documentation exists for new features (be nice to people who read your code), andcontact information is maintained (for all contributors to the program).=head1 OverviewUsage: perl precheck.pl <text file(s)>precheck.pl copies all the strings out of a script into an output file.=head1 RequiresAt least one text file as an argument in the command line.A binary build of Perl, since this program is being tested and no executable exists yet(I'd recommend ActivePerl).=head1 ModifiesThe input file is not modified.=head1 EffectsA text file is generated containing all the strings in the script.The file is placed in the same directory as the original script and has the prefix 'C-'.Any previous files with the filename and prefix 'C-' will be copied over.=cutdie "Usage: perl precheck.pl <text file(s)>\n"	unless (@ARGV);while (@ARGV) {	my $file = shift @ARGV;		open(IN,$file) ||		die "Could not open '$file': $!";		open(OUT,">C-$file") ||		die "Could not create 'C-$file': $!";		while (<IN>) {		while (/".*"/) {			s/^[^"]*"([^"]*)"//;			print OUT "$1\n";		}	}		close(IN) ||		die "Could not close '$file': $!";		close(OUT) ||		die "Could not close 'C-$file': $!";}
Code:
use warnings;use strict;=head1 'postchck' by Dintiradan - Version 0.1Last updated July 28, 2006Contact me with any suggestions or problems at dintiradan(at)yahoo(dot)comFeel free to use and modify this program for your scripts, provided:Original documentation is preserved (unless it no longer applies),documentation exists for new features (be nice to people who read your code), andcontact information is maintained (for all contributors to the program).=head1 OverviewUsage: perl postchck.pl <text file(s)>postchck.pl recopies all the strings from a 'C-' file back into the original script.=head1 RequiresAt least one text file as an argument in the command line.Both the script and 'C-' file must be present in the same directory as postchck.pl.A binary build of Perl, since this program is being tested and no executable exists yet(I'd recommend ActivePerl).=head1 ModifiesThe input script has its strings replaced with those found in the 'C-' file.The 'C-' file is not modified.=head1 EffectsEach line in the 'C-' file is inserted into each string in the script.=cutdie "Usage: perl postchck.pl <text file(s)>\n"	unless (@ARGV);while (@ARGV) {	my $file = shift @ARGV;		open(SCRIPT,$file) ||		die "Could not open '$file' for reading: $!";		open(STRINGS,"C-$file") ||		die "Could not create 'C-$file': $!";		my @lines = ();		while (<SCRIPT>) {		my $line = "";				while (/".*"/) {			my $str = <STRINGS>;			chomp($str);			s/^([^"]*)"[^"]*"//;			$line .= $1 . '"' . $str . '"';		}				$line .= $_;		push @lines, $line;	}		close(SCRIPT) ||		die "Could not close '$file' for reading: $!";		close(STRINGS) ||		die "Could not close 'C-$file': $!";		open(SCRIPT,">$file") ||		die "Could not open '$file' for rewriting: $!";		foreach my $i (@lines) {		print SCRIPT $i;	}		close(SCRIPT) ||		die "Could not close '$file' for rewriting: $!";}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...