Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Cleaning Data Between Specified Columns

by Aragorn (Curate)
on Jan 27, 2003 at 20:41 UTC ( #230349=note: print w/replies, xml ) Need Help??

in reply to Cleaning Data Between Specified Columns

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://230349]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2018-12-18 10:54 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (79 votes). Check out past polls.