http://www.perlmonks.org?node_id=296677


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

Small problem. You may want to clear out the leading \b's after the while statement. Like many others on this thread, you've been bitten by the terminal's evaluation of \b.

#!/usr/bin/perl -l $s = "\bt\b\bhello"; sub smack { $a = $s; $a =~ s/^\cH+//; 1 while ($a =~ s/[^\cH]\cH//g); $a; } sub othersmack { $a = $s; 1 while ($a =~ s/[^\cH]\cH//g); $a =~ s/^\cH+//; $a; } print smack(); print othermack(); print "smack correct" unless grep {ord==ord"\b"} split//,smack(); print "othersmack correct" unless grep {ord==ord"\b"} split//,othersma +ck(); __END__ output: hello hello othersmack correct

However, getting rid of the alternation is a good bit quicker. =)

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1

  • Comment on Re: Re: Re: Re: Re: Re: should this backspace removal code be done better?
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Re: should this backspace removal code be done better?
by shenme (Priest) on Oct 05, 2003 at 10:18 UTC
    Hell yes!   It's over 4 times faster than even my attempt, with your repair making it correct.   Gratuitous benchmark below:
                   Rate     smack    new1      uk  badkcams othersmack
    smack         188/s        --    -96%    -97%      -97%       -99%
    new1         5088/s     2607%      --     -8%      -23%       -86%
    uk           5517/s     2835%      8%      --      -16%       -85%
    badkcams     6594/s     3409%     30%     20%        --       -82%
    othersmack  37394/s    19797%    635%    578%      467%         --
                
                   Rate     smack    new1      uk  badkcams othersmack
    smack        42.6/s        --    -23%    -29%      -43%       -93%
    new1         54.9/s       29%      --     -8%      -27%       -91%
    uk           59.6/s       40%      9%      --      -21%       -90%
    badkcams     75.0/s       76%     37%     26%        --       -88%
    othersmack    626/s     1372%   1040%    951%      735%         --
    
    (See shenme's scratchpad for oogly program)