Kipp, James wrote:
> Hi
> I have some C code that I need to convert to perl and I am pressed
> for time, which is why I am posting this.  All the code does is read
> each character from either STDIN or a file and replaces any ASCII
> control characters with a space.
> 
> Is the perl getc() function the best way to look at one char at a
> time? 
> 
> The C code looks like below. I have some ideas but I am not sure of
> the best way to represent these char ranges in perl
> --
>   while ( !feof(fin) ) {
>     ch=getc(fin);
> 
>     if ( (ch<=0176) && (ch>=040) || (ch=='\n') ) {
>       putc( ch, stdout );
>     } else {
>       putc( ' ', stdout );
>     }
> 
>   }

The following one-liner will do the trick:

 perl -pe 'BEGIN {$/=\8192} tr/\040-\176/ /c' myfile

The /c on the tr/// operator complements the search range, so everything
*not* in the range 040 to 176 gets changed to a space.

The BEGIN block sets $/ so that input gets processed in 8kb chunks,
regardless of the format of the input files. (Can that be done with a
command-line option to perl?)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to