Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Control characters

by sandy1028 (Sexton)
on Jun 05, 2009 at 12:01 UTC ( [id://768764] : perlquestion . print w/replies, xml ) Need Help??

sandy1028 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have to replace all the control characters like ^O,^N but the code below does not substitutes.
$arr[$i] =~ s/^\c[A-Z]//g;
If I use the code like below
$arr[$i]=~ s/\cO//g;
updates for only ctrlO. Please tell me how to update for all character

Replies are listed 'Best First'.
Re: Control characters
by JavaFan (Canon) on Jun 05, 2009 at 12:28 UTC
    Control characters are three stroke tokens, \cA, \cB, etc. You cannot put parts of the tokens and put them into a character class. Use:
    or an equivalent tr construct.

    Note also that your use of ^ means you are only going to replace matches at the beginning of the string - at most one character in your example. Which from your description doesn't seem to be want you want.

      From the OP, it's not entirely clear (at least, to me) just what the OPer wants, but if all control characters are to be deleted, the substitution  s/[[:cntrl:]]+//g; might better serve: the POSIX character class syntax responds to one or both (I forget which) of Unicode and locale variations.

      And, of course, the other point to make to the OPer is that the character set  [\cA-\cZ] does not represent all control characters even in the ASCII character set.

Re: Control characters
by Anonymous Monk on Jun 05, 2009 at 12:08 UTC
Re: Control characters
by tbone1 (Monsignor) on Jun 05, 2009 at 15:18 UTC
    I was going to suggest using regular expressions and the s/// command but the control characters make it rather tricky, since the ^ anchors a search at the beginning of a string. Quick and dirty, but maybe something like this?

    s/(.)?^O/\1/g; s/(.)?^N/\1/g;

    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

      s/(.)?^O/\1/g; s/(.)?^N/\1/g;
      But what do these statements really do?
      >perl -wMstrict -le "for my $str (@ARGV) { printf q{%s -> }, $str; $str =~ s/(.)?^O/\1/g; $str =~ s/(.)?^N/\1/g; print $str; } " OhNoOhNo NotOnNotOn \1 better written as $1 at -e line 1. \1 better written as $1 at -e line 1. Use of uninitialized value $1 in substitution iterator at -e line 1. OhNoOhNo -> hNoOhNo Use of uninitialized value $1 in substitution iterator at -e line 1. NotOnNotOn -> otOnNotOn