 |
 |
underline in Perl
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
Does anyone know how to underline in Perl?
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
I am using Perl, version 5.005_01 built for powerpc-machten (BSD 4.4).
I can’t use MacPerl because I use several switches on the command line when I run the complete program.
I’m not a student and this isn’t a homework assignment.
I want to underline only the column headings and not the spaces between, that I get from the output of this portion of
the program. The underlining varies; as much as 5 in the case of high frequency letters such as 11R13 to 3 for low
frequency letters like 5F7. I have tried ‘ ‘, “ “, and "\137" on line 29 which should show as:
push @line, sprintf('%2d%s%-2d', scalar @{$trigraphs{$k}}, $k, scalar keys %{$flanks{$k}});
and I can’t get it right. Perhaps my approach is wrong. I don’t want the underlines on another line, I want it to look
like the output you get from a word processor or the output from troff. Too bad I can’t show you, it doesn’t print when
I send this message. You will see what I mean if you run the program. The program is self contained. All you have to do
is name it, make it executable, and run it. Thanks for any help.
<code>
#! /usr/bin/perl -w
use strict;
# 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 @{$trigraphs{$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
</code>
Too bad the indenting isn't comming through.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
I've learned how to use vB Code to underline but I can't get the indenting for the code to come out right. This is what I want to acheive with the column headings.
I want to underline only the column headings and not the spaces between, that I get from the output of this portion of the program. The underlining varies; as much as 5 in the case of high frequency letters such as 11R13 to 3 for low frequency letters like 5F7. I have tried ‘ ‘, “ “, and "\137" on line 29 which should show as:
push @line, sprintf('%2d%s%-2d', scalar @{$trigraphs{$k}}, $k, scalar keys %{$flanks{$k}});
and I can’t get it right. Perhaps my approach is wrong.
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status:
Offline
|
|
My suggestion would be to use the Term::ANSIColor module to deal with the escape sequences. Here's a quick example I hacked up:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Term::ANSIColor;
my @headings = ( 'One', 'Two', 'Three' );
my @data = (
[ 1, 2, 3],
[ 4, 5, 6],
);
foreach my $heading (@headings) {
my $padding = ' ' x (7 - length $heading);
print $padding, colored($heading, 'underline'), ' ';
}
print "\n";
foreach my $rec (@data) {
foreach my $val (@$rec) {
print sprintf('%7d ', $val);
}
print "\n";
}
Hmm code blocks don't even preserve indentation...
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
Thanks for the code example. I also found that vB wouldn’t keep the code indents, that’s why I didn’t repost it. However it indented properly in the email message I got alerting me to your reply.
As for Term::ANSIColor, I have been unable to compile Perl 5.8 into MachTen; the reason I don’t have modules. http://perldoc.com/perl5.8.0/README.machten.html#top
One day I will get a new Mac and then I’ll have what I need. In the meantime, I’ll try to compile 5.8 again so I can experiment with Term::ANSIColor.
Did you try it out on my code?
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status:
Offline
|
|
Term::ANSIColor doens't require Perl 5.8. It's min required version is 5.001.
I'm curious what kind of machine and OS are you running this on?
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
It may work with my version of Perl but I don't have it in my distribution. When I try it with MacPerl, which is 5.6, I get:
Code:
[4mOne[0m [4mTwo[0m [4mThree[0m
1 2 3
4 5 6
The spaces between columns is actually wider than shown in the output, but they don't line up under the column headings and the column headings come out exactly as shown. I will tinker with this, but until I can compile a module into MachTen, I'm stuck. Others must have gotten it to compile so I must be doing something wrong.
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status:
Offline
|
|
Ok well the problem there is the terminal doesn't understand the escape sequences. If the terminal doesn't know the escape sequences I don't think you will get underlining no matter what what you try to do it.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
WJMOORE,
You have been a great help. Once I realized that the ANSI control codes were coming through, but my terminal wasn’t recognizing them, I started looking into the idea that somehow I’ve got to get TERM = "something" and EXPORT "something" into my .login or .cshrc (or if there is a terminfo/termcap) file.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
WJMOORE,
I forgot to answer your question as to what machine and OS I was using.
I am using a PowerMac G4 400, OS 9.2.2 and an Apple Studio Display 17in LCD.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
WJMOORE,
I was successful in building perl-5.6.2 on Machten 4.1.4. That is the highest version that is supported. That’s all it took for me to be able to underline because it contains the module Term::ANSIColor. Now all I have to do is to get it to work on my file. Thanks for your help.
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status:
Offline
|
|
That's good to hear, good luck.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
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:
Code:
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:
Code:
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:
Code:
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 a numerical values from the scalar part of my code (it still sees $k as letters). If I changed %d to %s and used:
Code:
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
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 @{$trigraphs{$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
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
WJMoore, just to finish this thread off, and to help anyone who might find themselves in a similar situation, here is the solution that works for me.
Code:
push @line,
" " x ( 2 - length scalar @{ $trigraphs{$k} } )
. colored( scalar @{ $trigraphs{$k} }, 'underline' )
. colored( $k, 'underline' )
. colored( scalar keys %{ $flanks{$k} }, 'underline' )
. " " x ( 2 - length scalar keys %{ $flanks{$k} } );
(Last edited by imdipped; Dec 19, 2006 at 06:55 PM.
(Reason:line above "push @line" not relevant))
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|