in reply to should this backspace removal code be done better?

If you don't have two backspaces in a row, this quick and dirty will do it:

$_ = "this is an\b correct usage"; s/.[\b]//g; print;

Note that \b is a word boundary within a regex but a backspace within a character class.

For a more correct solution, see the Unix col command or col for PPT. See also Removing characters.

Replies are listed 'Best First'.
Re: Re: should this backspace removal code be done better?
by smackdab (Pilgrim) on Oct 05, 2003 at 06:35 UTC
    Make sense, if only you could tell the regex engine to work left to right it would work...I can't assume the user wont hit a backspace twice...I dit it about 6 times typing this reply ;-)
      Out of the mouths of dab'es ! Isn't this a 'sexeger' situation?

      Maybe someone should check my results, 'cuz I can't believe that adding in the reverse's doesn't impact these timings more.   I added a different routine to the mix benchmarked by antirice:

      sub badkcams { $a = reverse $s; $a =~ s[\cH[^\cH]|\cH$][]g while 1+index $a, chr(8); reverse $a; }
      I then ran the benchmark again:
                 Rate    smack     new1       uk badkcams
      smack     191/s       --     -96%     -97%     -97%
      new1     5063/s    2547%       --      -7%     -25%
      uk       5469/s    2759%       8%       --     -19%
      badkcams 6715/s    3411%      33%      23%       --
                 Rate    smack     new1       uk badkcams
      smack    42.4/s       --     -22%     -30%     -44%
      new1     54.7/s      29%       --      -9%     -27%
      uk       60.2/s      42%      10%       --     -20%
      badkcams 75.3/s      78%      38%      25%       --
      
      Am I reading this right?   It's faster?!?
      (This is Perl 5.8.0 AS build 806 running on WinXP.)