#!/usr/bin/perl -w # Give numeric labels to spans of non-whitespace characters. use strict; sub output_span($) { my ($count) = @_; if( $count == 1 ) { print 'X'; } elsif( $count == 2 ) { print '[]'; } else # $count > 2 { my $span = '[' . ('-' x ($count - 2)) . ']'; my $label_size = length($count); substr($span, int(($count - $label_size) / 2), $label_size) = $count; print $span; } } while( my $line = <> ) { chomp $line; my $count = 0; foreach my $c (map {chr} unpack 'C*', $line) { if( $c =~ /\s/ ) { if( $count > 0 ) { output_span($count); $count = 0; } print $c; } else { $count++; } } if( $count > 0 ) { output_span($count); } print "\n"; }