[ale] Stripping out crap characters in perl

Keith.Watson at gtri.gatech.edu Keith.Watson at gtri.gatech.edu
Wed Sep 7 13:50:30 EDT 2005


> -----Original Message-----
> From: ale-bounces at ale.org [mailto:ale-bounces at ale.org] On Behalf Of
> Christopher Fowler
> Sent: Wednesday, September 07, 2005 11:03
> To: ale at ale.org
> Subject: [ale] Stripping out crap characters in perl
> 
> I have written this program that interfaces with a modem and reads
data
> from a remote device.  The remote device dials in and dumps stuff out.
> As you know with modems that sometimes garbage is seen on connect and
> disconnect.  I store all my data in a variable "$buffer". Is there a
> function in perl that can go through an delete all those bad
characters
> from the normal ones that can be written to a file and read by any
text
> editor.  isprint() from ctype is not good because it will strip '\r'
and
> '\n' and those are valid characters.
> 

Christopher,

As with any thing in Perl there are lots of ways to do it, here's two.

my $buffer;

for (0..255) {

   $buffer .= chr ($_);
}


$buffer =~ s/[^\040-\176\12\15]//g;

print ($buffer);



This sets $buffer to all the ASCII characters from 0 to 255.
Then removes any characters that are NOT space to ~ (32 to 126) and not
LF (10) and CR (13). The corollary would be

$buffer =~ s/[\000-\011\013\014\016-\037\177-\377]//g;

This removes characters 0 to 9, 11, 12, 14 to 31, 127 to 255.

keith

-- 

Keith R. Watson                        GTRI/ISD
Systems Support Specialist III         Georgia Tech Research Institute
keith.watson at gtri.gatech.edu           Atlanta, GA  30332-0816
404-894-0836



More information about the Ale mailing list