Eric Sand wrote:
>
> Hi All,
Hello,
> I am very new to Perl,
Welcome. :-)
> but I sense a great adventure ahead after just
> programming with Cobol, Pascal, and C over the last umpteen years.
"Thinking in Perl" may take a while but it is not your grandfather's
programming language (sorry COBOL.)
> I have
> written a perl script where I am trying to detect a non-printing
> character(Ctrl@ - Ctrl_)
Your idea of non-printing seems to conflict with industry standards as
CtrlG - CtrlM are all printable. Also you are using perl's standard
readline and chomp()ing the input so you are not converting the CtrlJ
character at all.
> and then substitute a printing ASCII sequence such
> as "^@" in its place, but it does not seem to work as I would like. Any
> advice would be greatly appreciated.
use warnings;
use strict;
> $in_ctr=0;
> $out_ctr=0;
Whitespace is free and makes your code more readable and maintainable.
my $in_ctr = 0;
my $out_ctr = 0;
> while ($line = <STDIN>)
> {
> chomp($line);
> $in_ctr ++;
> if ($line = s/\c@,\cA,\cB,\cC,\cD,\cE,\cF,\cG,\cH,\cI,\cJ,\cK,
> \cL,\cM,\cN,\cO,\cP,\cQ,\cR,\cS,\cT,\cU,\cV,\cW,
> \cX,\cY,\cZ,\c[,\c\,\c],\c^,\c_
> /^@,^A,^B,^C,^D,^E,^F,^G,^H,^I,^J,^K,
> ^L,^N,^N,^O,^P,^Q,^R,^S,^T,^U,^V,^W,
> ^X,^Y,^Z,^[,^\,^],^^,^_/)
As Rob pointed out, this is not the correct way to use the substitution
operator (see below.)
> {
> $out_ctr ++;
> printf("Non-printing chars detected in: %s\n",$line);
You shouldn't use printf unless you really have to and in this case you
don't really have to.
print "Non-printing chars detected in: $line\n";
> }
> }
> printf("Total records read = %d\n",$in_ctr);
> printf("Total records written with non-printing characters =
> %d\n",$out_ctr);
print "Total records read = $in_ctr\n";
print "Total records written with non-printing characters = $out_ctr\n";
I would probably write it like this:
use warnings;
use strict;
my $out_ctr = 0;
while ( <STDIN> ) {
next unless s/([[:cntrl:]])/'^' . ( $1 | "\x40" )/eg;
$out_ctr++;
print "Non-printing chars detected in: $_\n";
}
print "Total records read = $.\n";
print "Total records written with non-printing characters = $out_ctr\n";
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>