D
Default
Hi, I'm totally new to programming and ive been reading the ng for a week or so now.
The things ive read in here have been very helpfull.
I just finished this little program that fixes letter case, im not sure if its bulletproof yet.
Anyhow it was kind of a pain as I've never delt with RegEx before, so if this is usefull to anyone
please by all means have it.
If there are errors please let me know. Thanks everybody you all are very cool.
#!
use strict;
use warnings;
print "\n" . " " . "="x78 . "\n";
print " Reads a text file and capitalizes the first letter of each sentence.\n\n";
print "\tUSAGE:\t perl 15_1.plx <inputfile> <outputfile> [options]\n";
print "\tNOTE:\t wildcards are not allowed.\n";
print "\tOPTIONS: -l Lowercases everything before doing the capitalization.\n";
print "\t\t -d Display work being done on screen.\n";
print "\n\tEXAMPLE: perl 15_1.plx fixcase.txt casefixed.txt\n";
print " " . "="x78 . "\n\n";
our $lines = "";
our $options1 = "";
our $options2 = "";
our $inputfile = shift || die "\n";
our $outputfile = shift || die "\n";
$options1 = shift || $options1 eq "";
$options2 = shift || $options2 eq "";
$options1 =~ tr/A-Z/a-z/;
$options2 =~ tr/A-Z/a-z/;
open_files($inputfile, $outputfile);
while (our $line = <INPUT>)
{
$lines = "$lines $line";
}
print "$lines\n\n" if ($options1 eq "-d" || $options2 eq "-d");
$lines =~ s/(\w+)/\L$1/g if ($options1 eq "-l" || $options2 eq "-l");
print "$lines\n\n" if ($options1 eq "-d" || $options2 eq "-d");
$lines =~ s/(\w)/\u$1/;
print "$lines\n\n" if ($options1 eq "-d" || $options2 eq "-d");
$lines =~ s/([\.\?\!]+\s+)(\w+)/$1\u\L$2/g;
print "$lines\n\n" if ($options1 eq "-d" || $options2 eq "-d");
print OUTPUT $lines;
print "\nDone.\n";
close (INPUT) || die "Can't close $inputfile $!\n";
close (OUTPUT) || die "Cant't close $outputfile $!\n";
#Subroutines#
sub open_files
{
open(INPUT, $inputfile) || die "\nError:\nCould not open $inputfile $!\n";
if (-e $outputfile)
{
die "\nError:\nFile already exists.\t$outputfile\n";
}
else
{
open(OUTPUT,">$outputfile") || die "Could not create $outputfile $!\n";
}
}
The things ive read in here have been very helpfull.
I just finished this little program that fixes letter case, im not sure if its bulletproof yet.
Anyhow it was kind of a pain as I've never delt with RegEx before, so if this is usefull to anyone
please by all means have it.
If there are errors please let me know. Thanks everybody you all are very cool.
#!
use strict;
use warnings;
print "\n" . " " . "="x78 . "\n";
print " Reads a text file and capitalizes the first letter of each sentence.\n\n";
print "\tUSAGE:\t perl 15_1.plx <inputfile> <outputfile> [options]\n";
print "\tNOTE:\t wildcards are not allowed.\n";
print "\tOPTIONS: -l Lowercases everything before doing the capitalization.\n";
print "\t\t -d Display work being done on screen.\n";
print "\n\tEXAMPLE: perl 15_1.plx fixcase.txt casefixed.txt\n";
print " " . "="x78 . "\n\n";
our $lines = "";
our $options1 = "";
our $options2 = "";
our $inputfile = shift || die "\n";
our $outputfile = shift || die "\n";
$options1 = shift || $options1 eq "";
$options2 = shift || $options2 eq "";
$options1 =~ tr/A-Z/a-z/;
$options2 =~ tr/A-Z/a-z/;
open_files($inputfile, $outputfile);
while (our $line = <INPUT>)
{
$lines = "$lines $line";
}
print "$lines\n\n" if ($options1 eq "-d" || $options2 eq "-d");
$lines =~ s/(\w+)/\L$1/g if ($options1 eq "-l" || $options2 eq "-l");
print "$lines\n\n" if ($options1 eq "-d" || $options2 eq "-d");
$lines =~ s/(\w)/\u$1/;
print "$lines\n\n" if ($options1 eq "-d" || $options2 eq "-d");
$lines =~ s/([\.\?\!]+\s+)(\w+)/$1\u\L$2/g;
print "$lines\n\n" if ($options1 eq "-d" || $options2 eq "-d");
print OUTPUT $lines;
print "\nDone.\n";
close (INPUT) || die "Can't close $inputfile $!\n";
close (OUTPUT) || die "Cant't close $outputfile $!\n";
#Subroutines#
sub open_files
{
open(INPUT, $inputfile) || die "\nError:\nCould not open $inputfile $!\n";
if (-e $outputfile)
{
die "\nError:\nFile already exists.\t$outputfile\n";
}
else
{
open(OUTPUT,">$outputfile") || die "Could not create $outputfile $!\n";
}
}