Insert newline into long string 
Author Message
 Insert newline into long string

A few weeks I asked a question: how can I insert a newline at certain intervals
in a long string. One of your great guys replied: Use

$longstring =~ s/.{0,70}/$1\n/g;

That is rather good. But I met a new problem: sometimes it simply insert
a newline in the middle of a word. How can I avoid this ?

Any help will be appreciated !

Sun



Sun, 12 Aug 2001 03:00:00 GMT  
 Insert newline into long string
:A few weeks I asked a question: how can I insert a newline at certain
intervals
:in a long string. One of your great guys replied: Use
:$longstring =~ s/.{0,70}/$1\n/g;
:That is rather good. But I met a new problem: sometimes it simply insert
:a newline in the middle of a word. How can I avoid this ?
:Any help will be appreciated !

The short answer is modules are your friends, see Text::Wrap.

The long answer:  Once upon a time in this news group Tim Gim Yee said that a
cat walked across his keyboard and came up with:

sub linewrap {
    my $line = shift; defined $line or return '';

    my $columns = shift || $columns;
    my $tabstop = shift || $tabstop;
    my $frag = '';
    my $col  = $columns - 1;


        $_ = "$frag$_";
        $frag = '';
        s/(.{1,$columns}$)|(.{1,$col}(?:\S\s+|-(?=\w)))|(.{$col})/
            $3 ? "$3-\n" :
            $2 ? "$2\n" :
            (($frag = $1), '')
        /ge;
        $frag .= (' ' x ($tabstop - length($frag) % $tabstop));
    }


    s/\s+$//gm;
    return $_;

Quote:
}

Pretty cool eh?.  Just to be pedantic it you use it like:
$string = "\txxxxxx ssssssss ddddddddddds ssssssssssss eeeeeeeeee
rrrrrrrrrrrrr eeeeeeeeeeeeees   rrrrrrrrr rrrrrrrrss   rrrrrrrrr ssssssssss
ddddd";

$columns = 70;
$tabstop = 5;
$out = linewrap($string);
print $out;

HTH

AmD
--

--random quote--
As far as the laws of mathematics refer to reality, they are not
certain; as far as they are certain, they do not refer to reality.
  Albert Einstein



Sun, 12 Aug 2001 03:00:00 GMT  
 Insert newline into long string

Quote:
> A few weeks I asked a question: how can I insert a newline at certain intervals
> in a long string. One of your great guys replied: Use

> $longstring =~ s/.{0,70}/$1\n/g;

> That is rather good. But I met a new problem: sometimes it simply insert
> a newline in the middle of a word. How can I avoid this ?

> Any help will be appreciated !

Somewhat simplemindedly:

$longstring =~ s/(.{0,70}) +/$1\n/g;

But of course you really want to be using Text::Wrap if you want to do it
nicely.

/J\
--

Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
Hastings: <URL:http://www.newhoo.com/Regional/UK/England/East_Sussex/Hastings>



Thu, 16 Aug 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. insert newlines in a long string

2. How to insert newline break into a long string

3. Adding newlines to long strings

4. inserting a long string into access w/perl

5. How to insert a newline in Perl

6. Can't insert newline

7. Oraperl - Insert to a LONG type

8. How to Insert Value in Long Columns of Oracle Table

9. Perl-Mysql - inserting long fields

10. Long insert feilds with ODBC/Access

11. Insert data into long raw column in oracle table

12. Long insert feilds with ODBC/Access

 

 
Powered by phpBB® Forum Software