ajr has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to find the visible length of a string (I print the same number of backspaces as the string, so I can provide a crude 'progress meter').
The addition of colour codes has really complicated things as "\033[32m" doesn't display on screen, yet length("\033[32m") returns 5.
I have got round the problem with:
Is there a better way of doing this?#!/usr/bin/perl -w use strict; my $str = "\033[32m coloured! \033[m"; my $visible_length = length( $str ); while ( $str =~ /\033\[\d*m/g ) { $visible_length -= length($&); } print "Visible Length = $visible_length (should be 11)\n";
Edit by tye to put [ inside CODE tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: finding visible length of string?
by BrowserUk (Patriarch) on Sep 18, 2002 at 23:51 UTC | |
by sauoq (Abbot) on Sep 19, 2002 at 00:16 UTC | |
by ajr (Initiate) on Sep 19, 2002 at 01:02 UTC | |
|
Re: finding visible length of string?
by greenFox (Vicar) on Sep 19, 2002 at 01:45 UTC | |
|
Re: finding visible length of string?
by alien_life_form (Pilgrim) on Sep 19, 2002 at 07:18 UTC | |
by BrowserUk (Patriarch) on Sep 19, 2002 at 16:20 UTC | |
by Aristotle (Chancellor) on Sep 19, 2002 at 19:35 UTC | |
by BrowserUk (Patriarch) on Sep 19, 2002 at 20:00 UTC | |
|
Re: finding visible length of string?
by jsprat (Curate) on Sep 19, 2002 at 17:33 UTC |