Taking one of the suggestions above, keeping your program flow intact, and mixing it a bit with my personal style, I come up with this:
#!/usr/bin/perl
use strict;
use warnings;
# Store the ranges in a list of hashes.
my @Ranges = ();
foreach my $arg (@ARGV) {
my ($start, $end) = split(/-/, $arg);
push @Ranges, { start => $start, end => $end };
}
# Use standard input/output so we can pipe files through it.
while (my $line = <STDIN>) {
# We go through the columns in reverse so the offsets will be
# correct.
foreach my $range (reverse @Ranges) {
my ($start, $end) = ($range->{start}, $range->{end});
my $line_len = length($line);
$end = $line_len if $line_len < $end;
next if $start > $end; # Skip out-of-bound ranges
my $nchars = $end - $start;
substr($line, $start-1, $end-$start) =
cleanse(substr($line, $start-1, $end-$start));
}
print $line;
}
# Remove apostrophes and other unwanted characters.
sub cleanse {
my $string = shift;
$string =~ s/'//g;
$string =~ tr/a-zA-Z0-9\n\|\-/ /c;
return $string;
}
It could use some extra checking on the program arguments (ranges), though.
Arjen
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|