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....


In reply to Re: wrapping by demerphq
in thread wrapping a pair of lines; what if different lengths? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.