I have been able to underline the column headings using Term::ANSIColor but I am not satisfied with the result. There can be twenty six columns depending on the data, so I want to keep my output width to a minimum. The column header consists of one or two digits (the count of the letter) followed by the letter followed by one or two digits called flank in the code (the count of how many times the letter contacts a different letter). The headers vary in width from five to three characters.
The line (32) in question is:
push @line, sprintf('%2d%s%-2d', scalar @{$trigraphs{$k}}, $k, scalar keys %{$flanks{$k}});
When I use colored in the format portion of sprintf(), like this:
sprintf(colored('%2d%s%-2d', 'underline'),
I always get an underline that is five characters wide, which is good when my column header is five characters wide; not so good when the header is only three characters wide. I was forced to go that route because even though I got the correct results with the letter part of the header, like this:
colored($k, 'underline'),
I was unable to get around the fact that I couldn’t use the %d identifier when I tried to underline what I thought would be numerical values from the scalar part of my code (it still sees $k as letters). If I changed %d to %s and used:
colored(scalar @{$trigraphs{$k}}, 'underline'), and
colored(scalar keys %{$flanks{$k}}, 'underline'));
Note: the last ) is to match the ( in sprintf().
I could get the underline I want, but the headers no longer lined up with the letter above it’s corresponding letter in the columns. The columns print as they should with three spaces between them.
That is my problem, can anyone help? Should anyone want to run the code
#! /usr/bin/perl
use strict;
use warnings;
use Term::ANSIColor;
# get cipherText
my $txt = do{local $/; <DATA>};
$txt =~ s/\s//go;
my @letters = split '', $txt;
# create trigraphs
my (%trigraphs, %flanks);
add_trigraph('-', @letters[0..1]);
while(@letters > 2){
add_trigraph(@letters[0..2]);
shift @letters;
}
add_trigraph(@letters[0..1], '-');
# sort by most seen middle character
my @sorted_keys = sort{
@{$trigraphs{$b}} <=> @{$trigraphs{$a}} || $a cmp $b
} keys %trigraphs;
# create columns/lines
for(my $i = -1;$i < @{$trigraphs{$sorted_keys[0]}};++$i){
my @line;
for my $k (@sorted_keys){
unless($i > -1){ # create header
@{$trigraphs{$k}} = sort @{$trigraphs{$k}};
push @line, sprintf('%2d%s%-2d', scalar @{$tri
+graphs{$k}}, $k, scalar keys %{$flanks{$k}});
next;
}
next unless defined $trigraphs{$k}->[$i];
push @line, sprintf(' %s ', $trigraphs{$k}->[$i]);
}
print "@line\n";
}
sub add_trigraph {
++$flanks{$_[1]}->{$_} for @_[0,2];
push(@{$trigraphs{$_[1]}}, join('', @_));
}
__DATA__ # elcy pg.73 in P format
FDRJN UHVXX URDMD SKVSO PJRKZ DYFZJ
XGSRR VTQYR WDARW DFVRK VDRKV TDFSZ
ZDYFR DNNVO VTSXS AWVZR
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.