Madhu Reddy wrote:
> 
> Hi,

Hello,

>   I want to write following C Equivalant with PERL
> My perl equvalant is at the end of this mail..
> please correct me...
> my questios are inside perl comments...
> 
> ----------
> C Code start
> -----
> for(i=0;i<(int)strlen(Buffer);i++)
>  {
>   if((Buffer[i] <  32) || (Buffer[i] > 126))
>    {
>     if((Buffer[i] == 10) || (Buffer[i] == 13))
>      {
>       Buffer[i] = '\0';
>      }
>     else
>      {
>       BadRec = 1;
>       if(Buffer[0] != 26)
>        {
>         BadCnt++;
>         fprintf(Reject,"%s\n",Buffer);
>        }
>       else
>        Ignore++;
>      }
>     break;
>    }
>  }
> ----------------
>  PERL CODE START
> ---------------
> for(split(//)){
>  if ((ord() < 32) || (ord() < 126)) {
>         if (ord() == 10) || (ord() == 13)) {
>                 #make this char as NULL
>       # here how to make this char is NULL ?
>       # $_ =~ tr/\r\n/\0\0/; is this Correct ?
>         } else {
>                 $badrec = 1;
>                 # if ord() of first char is not 26
>                   # reject reocrd
> 
>         }
>  }
> }


A literal translation of the C code would be:

for ( my $i = 0; $i < length( $Buffer ); $i++ ) {
    if ( ord( substr( $Buffer, i, 1 ) ) < 32 or ord( substr( $Buffer, i, 1 ) ) > 126 ) 
{
        if ( ord( substr( $Buffer, i, 1 ) ) == 10 or ord( substr( $Buffer, i, 1 ) ) == 
13 ) {
            substr( $Buffer, i, 1 ) = "\0";
            }
        else {
            $BadRec = 1;
            if ( ord( substr( $Buffer, 0, 1 ) ) != 26 ) {
                $BadCnt++;
                print Reject "$Buffer\n";
                }
            else {
                $Ignore++;
                }
            }
        last;
        }
    }


Which would look like this in perl:

unless ( $buf =~ tr/\0-\x1F\x7F-\xFF// ) {
    unless ( $buf =~ tr/\x0A\x0D/\0/ ) {
        $BadRec = 1;
        if ( $buf =~ /^\x1A/ ) {
            $Ignore++;
            }
        else {
            $BadCnt++;
            print Reject "$buf\n";
            }
        }
    }



John
-- 
use Perl;
program
fulfillment

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

Reply via email to