in reply to trivial wrapping

If you don't mind destroying the strings as you output them, then this will do the job. If you need them for further processing then you could copy the array first. However, from the use of $genome as a variable name, it could well be that your strings are of extreme length, in which case copying them may be prohibitively expensive on memory. In which case, say so and someone will suggest a non-destructive way. Its not much harder

#! perl -sw use strict; my @strings = map{"$_" x 200} 1 .. 3; while( length "@strings" > 2 ) { print substr( $strings[$_], 0, 60, '') . "\n" for 0 .. $#strings; print "\n"; } __END__ c:\test>213729 111111111111111111111111111111111111111111111111111111111111 222222222222222222222222222222222222222222222222222222222222 333333333333333333333333333333333333333333333333333333333333 111111111111111111111111111111111111111111111111111111111111 222222222222222222222222222222222222222222222222222222222222 333333333333333333333333333333333333333333333333333333333333 111111111111111111111111111111111111111111111111111111111111 222222222222222222222222222222222222222222222222222222222222 333333333333333333333333333333333333333333333333333333333333 11111111111111111111 22222222222222222222 33333333333333333333 c:\test>

Update: I noticed the mention of $genome1 after my first attempt at this and then disliked the destructive nature of the code and also that the loop condition would be exspensive on memory for large strings and/or arrays, so here's a better version without those caveats. The output is the same.

#! perl -sw use strict; my @strings = map{"$_" x 200} 1 .. 3; my ($total, $p) = (0, 0); do { $total = 0; print substr( $strings[$_], $p, 60 ) . "\n" for 0 .. $#strings; print "\n"; $p+=60; $total += length($_) - $p for @strings; } while( $total > 0 ); __END__

Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Replies are listed 'Best First'.
Re: Re: trivial wrapping
by GhodMode (Pilgrim) on Nov 18, 2002 at 14:46 UTC

         That way is much too complicated. A format will serve the purpose very well in this case...

    format FH = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~ $var1 . $var1 = "hahhahahahaThis is my great big long string of more " . "than 60 characters hahahahahahahahahahahahahaahahahhahah" ; open (FH, "> somefile.txt"); write FH; close FH;

    Summary:
    1. The circumflex (^) indicates that it is a variable length record
    2. The less-than signs (<) left-justify the printed value
    3. The squiggles (tildes (~)) suppress blank lines and keep printing lines until the variable is empty.
    4. This also destroys the variable

         I got this directly from the Camel's mouth... All hail the Camel (O'Reilly Programming Perl).

    Invulnerable. Unlimited XP. Unlimited Votes. I must be...
            GhodMode

      Now show me a FORMAT that will wrap two strings to 60 wide and interleave them as requested by the OP and I'll buy you a coffee next time I see you:)


      Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
      Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
      Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
      Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

        I like strong coffee, no cream, lotsa sugar :) ...

        format FILE = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $var1 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $var2 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $var3 ------------------------------------------------------------ ------------------------------------------------------------ . $var1 = "11111111111111111111". "11111111111111111111". "11111111111111111111". "11111111111111111111". "11111111111111111111". "11111111111111111111". "11111111111111111111". "11111111111111111111". "11111111111111111111"; $var2 = "22222222222222222222". "22222222222222222222". "22222222222222222222". "22222222222222222222". "22222222222222222222". "22222222222222222222". "22222222222222222222". "22222222222222222222". "22222222222222222222"; $var3 = "33333333333333333333". "33333333333333333333". "33333333333333333333". "33333333333333333333". "33333333333333333333". "33333333333333333333". "33333333333333333333". "33333333333333333333". "33333333333333333333"; open (FILE, "> file.txt") or die "Could not open file\n$!\n"; while ( ($var1 ne "") and ($var2 ne "") and ($var3 ne "") ){ write FILE;

        It's a little more complex, but I still like the format better :)
        Here's the output...
        cat file.txt 111111111111111111111111111111111111111111111111111111111111 222222222222222222222222222222222222222222222222222222222222 333333333333333333333333333333333333333333333333333333333333 ------------------------------------------------------------ ------------------------------------------------------------ 111111111111111111111111111111111111111111111111111111111111 222222222222222222222222222222222222222222222222222222222222 333333333333333333333333333333333333333333333333333333333333 ------------------------------------------------------------ ------------------------------------------------------------ 111111111111111111111111111111111111111111111111111111111111 222222222222222222222222222222222222222222222222222222222222 333333333333333333333333333333333333333333333333333333333333 ------------------------------------------------------------ ------------------------------------------------------------ 111111111111111111111111111111111111111111111111111111111111 222222222222222222222222222222222222222222222222222222222222 333333333333333333333333333333333333333333333333333333333333 ------------------------------------------------------------ ------------------------------------------------------------ 1111111111111111111111111111111111111111 2222222222222222222222222222222222222222 3333333333333333333333333333333333333333 ------------------------------------------------------------ ------------------------------------------------------------

        Invulnerable. Unlimited XP. Unlimited Votes. I must be...
                GhodMode
Re: Re: trivial wrapping
by Anonymous Monk on Nov 18, 2002 at 13:55 UTC
    Hi BrowserUk, thanks for your help. Unfortunatley I am having problems getting this to work.. i dont completely understand your code and cant see where to plug in my two strings ($genome1 and $genome2). Can i do ;
    my @strings = map {$genome1, $genome2};
    or something like? thanks again ;-)

      You dont need the map, a simple array assignment will do.

      my @strings = ($genome1, $genome2);

      Update. That said, if you don't have your strings in an array, and its difficult to put them into one to start with, a small modification to the routine will prevent needless duplication.

      #! perl -sw use strict; # ... your existing code.... my @strings = \($genome1, $genome2); my ($total, $p) = (0, 0); do { $total = 0; #NOTE: Extra $'s v ........................................v print substr( $$strings[$_]}, $p, 60 ) . "\n" for 0 .. $#$strings; print "\n"; $p+=60; $total += length($_) - $p for @$strings; # And here! } while( $total > 0 ); __END__

      Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
      Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
      Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
      Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.