matching for specific ascii decimal "non-printable character"

J

Jack

Hi

I have "decimal 28 nonprintables" in my file as delimiters, this I
know.
How do I match to it ? I tried the following and they dont seem to be
detecting - any ideas ?

if ($_ =~ m/\028/) { print " a "; }
if ($_ =~ m/028/) { print " 0 "; }
if ($_ =~ m/28/) { print " 1 "; }
if ($_ =~ m/-d '\028'/) { print " 2 ";}

Also, does someone know how to detect newlines (carriage returns) as
the first character in a string (in this case a variable where I read
in a file record... I tried the below and it too is not detecting !

if ($newline =~ m/^\n/) { print " newline " ; }

Many Thanks,
Jack
 
J

John W. Krahn

Jack said:
I have "decimal 28 nonprintables" in my file as delimiters, this I
know.
How do I match to it ? I tried the following and they dont seem to be
detecting - any ideas ?

if ($_ =~ m/\028/) { print " a "; }
if ($_ =~ m/028/) { print " 0 "; }
if ($_ =~ m/28/) { print " 1 "; }
if ($_ =~ m/-d '\028'/) { print " 2 ";}

If you want to use an octal escape sequence then you have to convert the
decimal number to octal:

$ perl -e'printf "%o\n", 28'
34

And then you can use that:

if ( /\034/ ) { print ' a ' }

Or you could convert it to hexadecimal:

$ perl -e'printf "%X\n", 28'
1C

And use that:

if ( /\x1C/ ) { print ' a ' }

Or since it is an ASCII control character you could use that:

if ( /\c\\\(?:)/ ) { print ' a ' }

(Ugly I know but the backslashes screw things up.)

Also, does someone know how to detect newlines (carriage returns) as
the first character in a string (in this case a variable where I read
in a file record... I tried the below and it too is not detecting !

if ($newline =~ m/^\n/) { print " newline " ; }

Newlines and carriage returns are (usually) two different things. You may
need to use:

if ( $newline =~ m/^\r/ ) { print " newline " }



John
 
B

Bart Van der Donck

Jack said:
I have "decimal 28 nonprintables" in my file as delimiters, this I
know.
How do I match to it ? I tried the following and they dont seem to be
detecting - any ideas ?
if ($_ =~ m/\028/) { print " a "; }
if ($_ =~ m/028/) { print " 0 "; }
if ($_ =~ m/28/) { print " 1 "; }
if ($_ =~ m/-d '\028'/) { print " 2 ";}

if (/\x@{[sprintf'%x',28]}/i) { print 'OK' }
Also, does someone know how to detect newlines (carriage returns) as
the first character in a string (in this case a variable where I read
in a file record... I tried the below and it too is not detecting !
if ($newline =~ m/^\n/) { print " newline " ; }

if ($newline=~/^(\x0A|\x0D|\x85|\x0C)/i) { print 'OK' }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top