Mike McClain wrote:
On Thu, Oct 28, 2010 at 07:28:19PM +0530, perl_haxor 123 wrote:
characters and how can remove them using perl?.....and also please let me if
there is any link form where i can find what these characters are and their
ascii values?......any sugges would be really helpful
# man ascii
If that fails:
<code>
#!/usr/bin/perl -Tw
# ascii Thu Dec 11 2008 (c) Mike McClain
# print ascii chart, oct, hex, dec ala, /mc/docs/ascii.gz
# re: man ascii
use strict; # this can be removed when all bugs are fixed
use warnings; # duplicates -w
It does not duplicate -w, in fact -w has no effect after this point in
your program. See:
perldoc perllexwarn
use integer;
$|++; # unbuffered STDOUT
STDOUT still buffered but now autoflushed.
# requires explicit package name
my ($hi_ascii, $do_oct, $do_hex, $do_dec, $do_ctrl) = (0,0,0,0,0);
## get CL switches
$hi_ascii = 1 if( @ARGV&& ($ARGV[0] =~ /h/i) );
$do_oct = 1 if( @ARGV&& ($ARGV[0] =~ /o/i) );
$do_hex = 1 if( @ARGV&& ($ARGV[0] =~ /x/i) );
$do_dec = 1 if( @ARGV&& ($ARGV[0] =~ /d/i) );
$do_ctrl = 1 if( @ARGV&& ($ARGV[0] =~ /c/i) );
Because the match operator returns true or false in scalar context you
could write that as:
## get CL switches
my $hi_ascii = @ARGV && $ARGV[0] =~ /h/i;
my $do_oct = @ARGV && $ARGV[0] =~ /o/i;
my $do_hex = @ARGV && $ARGV[0] =~ /x/i;
my $do_dec = @ARGV && $ARGV[0] =~ /d/i;
my $do_ctrl = @ARGV && $ARGV[0] =~ /c/i;
(And shouldn't that be 'a' for ASCII and 'h' for hexadecimal?)
my $sep = '|';
my @symbols = qw/
nul soh stx etx eot enq ack bel
bs ht nl vt np cr so si
dle dc1 dc2 dc3 dc4 nak syn etb
can em sub esc fs gs rs us
sp del/;
^^^^
Since when is the space character a control character?
[ SNIP ]
sub print_ascii
{ my ($mode, $hi) = @_;
$mode =~ /^[dox]$/ || die "Unknown mode in print_ascii($mode) \n";
$mode = ( $mode =~ /d/) ? '%3d' : ( $mode =~ /x/) ? ' %02x' : '%03o';
unless($hi)
{ for my $i ( 0 .. 127)
{ if( $i< 33)
{ printf( "$sep$mode %-3s", $i, $symbols[$i]); }
^^^^
elsif( $i == 127)
{ printf( "$sep$mode del", $i ); }
^^^^
else
{ printf( "$sep$mode %s ", $i, chr($i) ); }
^^^^
( (($i + 1) % 8) == 0)&& print "|\n";
}
}
else
{ for my $i ( 128 .. 255)
{ printf( "|$mode %s ", $i, chr($i) );
^
One of these printf format strings is not like the other.
And why are $sep and @symbols in file scope when they are only used
inside this subroutine?
And why is this subroutine *only* used for its side effect instead of
having it return a value?
( (($i + 1) % 8) == 0)&& print "|\n";
}
}
}
__END__
</code>
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/