Perl Script to Remove Strings

Error message

Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /var/www/pied-piper.ermarian.net/includes/common.inc).
AuthorTopic: Perl Script to Remove Strings
Guardian
Member # 6670
Profile Homepage #0
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:print OUT "$1\n";When I removed OUT (getting print to go to STDOUT), I got:Use of uninitialized value in concatenation (.) or stringAnyways, any help would be appreciated; Word doesn't catch all the errors when the strings are surrounded by 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': $!";
}

Posts: 1509 | Registered: Tuesday, January 10 2006 08:00
Off With Their Heads
Member # 4045
Profile Homepage #1
Well, astring already does this. I can't find a download location anymore, possibly because Khoth's site went down.

--------------------
Arancaytar: Every time you ask people to compare TM and Kel, you endanger the poor, fluffy kittens.
Smoo: Get ready to face the walls!
Ephesos: In conclusion, yarr.

Kelandon's Pink and Pretty Page!!: the authorized location for all things by me
The Archive of all released BoE scenarios ever
Posts: 7968 | Registered: Saturday, February 28 2004 08:00
Guardian
Member # 6670
Profile Homepage #2
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.
Posts: 1509 | Registered: Tuesday, January 10 2006 08:00
Guardian
Member # 6670
Profile Homepage #3
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. :P

--------------------
use warnings;
use strict;

=head1 'precheck' by Dintiradan - Version 0.1

Last updated July 28, 2006

Contact me with any suggestions or problems at dintiradan(at)yahoo(dot)com

Feel 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), and
contact information is maintained (for all contributors to the program).

=head1 Overview

Usage: perl precheck.pl <text file(s)>

precheck.pl copies all the strings out of a script into an output file.

=head1 Requires

At 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 Modifies

The input file is not modified.

=head1 Effects

A 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.

=cut

die "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': $!";
}
use warnings;
use strict;

=head1 'postchck' by Dintiradan - Version 0.1

Last updated July 28, 2006

Contact me with any suggestions or problems at dintiradan(at)yahoo(dot)com

Feel 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), and
contact information is maintained (for all contributors to the program).

=head1 Overview

Usage: perl postchck.pl <text file(s)>

postchck.pl recopies all the strings from a 'C-' file back into the original script.

=head1 Requires

At 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 Modifies

The input script has its strings replaced with those found in the 'C-' file.
The 'C-' file is not modified.

=head1 Effects

Each line in the 'C-' file is inserted into each string in the script.

=cut

die "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: $!";
}

Posts: 1509 | Registered: Tuesday, January 10 2006 08:00