mhearse has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to break up a string which is very long. I've come up with the following code. Is there a better/prettier way to do something like this?
#!/usr/bin/perl use strict; my $var = 'x' x 10000; my $var_length = length $var; my $max_length = 100; my $last_length = 0; while ($var_length > 100) { $last_length+=$max_length; $var_length-=$max_length; $var =~ s/\S{$last_length}/$&\n/sg; } print $var, "\n";

Replies are listed 'Best First'.
Re: Breaking up a really long string
by FunkyMonk (Bishop) on Jun 23, 2008 at 21:11 UTC
    I've tinkered with the string lengths to get something more manageble, but you should be able to see what's happening.

    See $INPUT_RECORD_SEPARATOR in perlvar for details of what $/ = \$max_length does. See open for a description of the effect of using a scalar reference instead of a filename.

    use strict; my $var = 'x' x 100; my $max_length = 10; $/ = \$max_length; # input record separator $\ = "\n"; # output record separator open my $FH, "<", \$var or die; print while <$FH>;

    update: Added link to open


    Unless I state otherwise, all my code runs with strict and warnings
Re: Breaking up a really long string
by johngg (Canon) on Jun 23, 2008 at 21:56 UTC
    Perhaps you could use unpack.

    $ perl -le ' > $str = q{x} x 97; > print for unpack q{(A10)*}, $str;' xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxx $

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Breaking up a really long string
by ikegami (Patriarch) on Jun 23, 2008 at 23:22 UTC
Re: Breaking up a really long string
by moritz (Cardinal) on Jun 23, 2008 at 21:26 UTC
    I had the same problem some time ago (I have IRC logs online, and too long words cause horizontal scrolling, which is evil, so I had to break up words), and I solved it roughly the same way as you did.

    I can only add that it's a bad idea to use $&, because it will slow down regex matches in your whole script, and even in modules included from your script.

    Use $var =~ s/(\S{$last_length})/$1\n/sg; instead.

      it's a bad idea to use $&

      Some more background as to why can be found here   (note to the OP — I'm sure you know :)

      Wow. Thanks for both of your posts. Unfortunately, I was unaware of the input/output record separators (which I'm ashamed to admit!). Thanks again.
Re: Breaking up a really long string
by caelifer (Scribe) on Jun 23, 2008 at 22:02 UTC
    You can also do (updated)
    use strict; local $\ = "\n"; local $_ = 'x' x 1000; my $pat = qr/.{1,80}/; while (/($pat)/g) { print $1; }
Re: Breaking up a really long string
by almut (Canon) on Jun 23, 2008 at 22:12 UTC

    Another one:

    my $var = 'x' x 10000; my $max_length = 100; my $i = $max_length; while ($i < length $var) { substr($var, $i, 0) = "\n"; $i += $max_length + 1; } print $var, "\n";
Re: Breaking up a really long string
by ikegami (Patriarch) on Jun 23, 2008 at 23:17 UTC

    The solutions others have presented are way too complex.

    while (my ($block) = $var =~ /(.{1,$max_length})/gs) { ... }

    Note that it's very bad to use $&. It slows down every regexp with no captures in your program.

    Update: It a better way of doing the same thing as FunkyMonk's, johngg's, caelifer's, almut's and jwkrahn's code, but our code doesn't do exactly the same thing as yours.

Re: Breaking up a really long string
by jwkrahn (Abbot) on Jun 24, 2008 at 01:51 UTC

    Another way to do it:

    my $var = 'x' x 1_000; my $var_length = length $var; my $max_length = 100; for ( my $offset = $var_length - $var_length % $max_length; $offset >= + 0; $offset -= $max_length ) { substr $var, $offset, 0, "\n"; } print "$var\n";
Re: Breaking up a really long string
by ysth (Canon) on Jun 24, 2008 at 15:47 UTC