I don't see why you're using a hash here. Your keys form a simple number sequence. Why not use an array? Also - why sort the keys? First of all, you're sorting alphanumerically, so it's not doing what you likely expected. Secondly, the order in which you apply the substitutions doesn't matter, so even a numerical sort would just be wasted effort. There is also no reason to initialize the arrays over and over each time you call the sub.

Finally, the name isn't very well chosen; I'd suggest Term::ANSI::Remove (for the current incarnation) instead.

Everything put together, I propose this as a starting point:

package Term::ANSI::Remove; use strict; use warnings; use vars qw(@ISA @EXPORT); use Exporter; @ISA = qw/Exporter/; @EXPORT = qw/escape_ansi/; my @remove = qw( \x1b\x5b\d+A \x1b\x5b\d+B \x1b\x5b\d+C \x1b\x5b\d+D \x1b\x5bK \x1bD \x1bM \x1b7 \x1b8 \x1b\x233 \x1b\x234 \x1b\x235 \x1b\x236 \x1b\x5b\d+\x3b[01457]+m \x1b\x5b0K \x1b\x5b2K \x1b\x5bJ \x1b\x5b0J \x1b\x5b2J \x1b\x5b\d\x3b[01234]+q \x1b[\x28\x29]A \x1b[\x28\x29]B \x1b[\x28\x29]0 \x1b[\x28\x29]1 \x1b[\x28\x29]2 \x1bK\d+\x38\d+r \x1bH \x1b\x5bg \x1b\x5b0g \x1b\x5b3g \x1b\x5b20h \x1b\x5b20l \x1b\x5b\x3f1h \x1b\x5b\x3f1l \x1b\x5b\x3f2l \x1b\x5b\x3f3h \x1b\x5b\x3f3l \x1b\x5b\x3f4h \x1b\x5b\x3f4l \x1b\x5b\x3f5h \x1b\x5b\x3f5l \x1b\x5b\x3f6h \x1b\x5b\x3f6l \x1b\x5b\x3f7h \x1b\x5b\x3f7l \x1b\x5b\x3f8h \x1b\x5b\x3f8l \x1b\x5b\x3f9h \x1b\x5b\x3f9l \x1b\x5b6n \x1b\x5b\d+\x3b(\d+)R \x1b\x5b5n \x1b\x5bc \x1b\x5b0c \x1b\x5bc \x1b\x5b0c \x1b\x5b\x3f1\x3b[0-7]c \x1bc \x1b\x238 \x1b\x5b2\x3b\d{1,3}y \x1bA \x1bB \x1b[A-DF-KZ12<>=] \x1bA[\x3e\x3d\x3c] \x1bY[\000-\377]{2} ); my %replace = ( qr/\x1b\x5b\d+\x3b\d+[fH]/ => '', qr/\x1b\x5b1/ => '', ); my $remove_rx = join '|', @remove; $remove_rx = qr/(?:$remove_rx)/; sub escape_ansi { my $buf_ref = shift; my ($ansi, $replace); $$buf_ref =~ s/$remove_rx//; $$buf_ref =~ s/$ansi/$replace/g while ($ansi, $replace) = each %replace; } 1;

Makeshifts last the longest.


In reply to Re: clean Telnet ANSI escape codes: VT52 and VT100 by Aristotle
in thread clean Telnet ANSI escape codes: VT52 and VT100 by JamesNC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.