Your approach is pretty C like. :-)
Heres two routines to do what you want. The first is task specific (itll only work on three lines). The second is generic and will work on any number of lines. They both work on the same principal. That is by choping off at most $max characters from the front of the string (actually a copy so the originals dont get toasted) and then printing them.
I got a touch carried away :-) and wrote the header printer as well. Also my Perl is fairly idiomatic. I hope its not too confusing. If it is then ask and I'll explain in detail.
Thanks by the way, this was fun.
use strict;
use warnings;
sub print_header {
my ( $max, $indent ) = @_;
$indent = " " x $indent;
my $maxlen=length $max;
# Create a header so they can see what column things are on
my @top;
foreach my $num ( 1 .. $max ) {
my $r = 0;
foreach my $char ( split //, sprintf( "% ${maxlen}s", $num ) )
+ {
$top[ $r++ ] .= $char;
}
}
# print the header
print $indent. join ( "\n" . $indent, @top ), "\n";
# print a divider line
print $indent. ( "-" x $max ) . "\n";
}
sub split_lines {
my ( $max, $x, $y, $z ) = @_;
# $max is the maximum length of a line
# $x,$y,$z are the lines to be split and displayed
print_header( $max, 2 ); # two because of the "> " from below
while ( length($x) || length($y) || length($z) ) {
# while any of our strings have content left within
# cut out the first $max chars and print them
# with a blank line seperating the groups
s/^(.{0,$max})//s
and print "> $1\n"
for $x, $y, $z;
print "\n";
}
}
sub generic {
my $max = shift;
my @lines = @_;
my $indent = length(@lines);
print_header( $max, $indent + 2 ); # +2 because of the "> " below
PRINT: {
# while any of our strings have content left within
# cut out the first $max chars and print them
# with a blank line seperating the groups
$lines[$_] =~ s/^(.{0,$max})//s
and printf "% ${indent}s> %s\n", $_ + 1, $1
for 0 .. $#lines;
print "\n";
length($_) and redo PRINT for @lines;
}
}
my $x = "CATGACTTCTAATCGCACTG";
my $y = " **** ****** *** *";
my $z = "CCATTCTGACTGACTTTCAG";
print "The pretty task specific version:\n";
split_lines( 15, $x, $y, $z );
print "The generic version:\n";
generic( 15, $x, $y, $z, $x, $y, $z, $x, $y, $z, $x, $y, $z );
__END__
The pretty task specific version:
111111
123456789012345
---------------
> CATGACTTCTAATCG
> **** ****** *
> CCATTCTGACTGACT
> CACTG
> ** *
> TTCAG
The generic version:
111111
123456789012345
---------------
1> CATGACTTCTAATCG
2> **** ****** *
3> CCATTCTGACTGACT
4> CATGACTTCTAATCG
5> **** ****** *
6> CCATTCTGACTGACT
7> CATGACTTCTAATCG
8> **** ****** *
9> CCATTCTGACTGACT
10> CATGACTTCTAATCG
11> **** ****** *
12> CCATTCTGACTGACT
1> CACTG
2> ** *
3> TTCAG
4> CACTG
5> ** *
6> TTCAG
7> CACTG
8> ** *
9> TTCAG
10> CACTG
11> ** *
12> TTCAG
HTH.
--- demerphq
my friends call me, usually because I'm late....
|