insert newlines in a long string 
Author Message
 insert newlines in a long string

Hello, how could i take a long string in perl
and insert newlines at the whitespace nearest
to 80 chars? So a string that contains 320 chars
would have roughly 5 newlines inserted into
it.


Fri, 24 Jun 2005 20:55:49 GMT  
 insert newlines in a long string

Quote:
>> On Mon, 06 Jan 2003 19:55:49 GMT,

> Hello, how could i take a long string in perl and insert
> newlines at the whitespace nearest to 80 chars? So a
> string that contains 320 chars would have roughly 5
> newlines inserted into it.

Text::Wrap is one way.

hth
t



Fri, 24 Jun 2005 21:03:01 GMT  
 insert newlines in a long string

Quote:
> Hello, how could i take a long string in perl
> and insert newlines at the whitespace nearest
> to 80 chars? So a string that contains 320 chars
> would have roughly 5 newlines inserted into
> it.

I think what you need exactly is that whatever be the whitespace just
before/closest to 80 character (column margin), it should be replaced by
\n...Right?

--
Perl is designed to give you several ways to do anything, so
consider picking the most readable one.
             -- Larry Wall in the perl man page



Fri, 24 Jun 2005 21:04:04 GMT  
 insert newlines in a long string

Quote:



>> Hello, how could i take a long string in perl
>> and insert newlines at the whitespace nearest
>> to 80 chars? So a string that contains 320 chars
>> would have roughly 5 newlines inserted into
>> it.

> I think what you need exactly is that whatever be the whitespace just
> before/closest to 80 character (column margin), it should be replaced by
> \n...Right?

Yes, that is correct.
Quote:

> --
> Perl is designed to give you several ways to do anything, so
> consider picking the most readable one.
>              -- Larry Wall in the perl man page



Fri, 24 Jun 2005 21:13:01 GMT  
 insert newlines in a long string

Quote:

>>> On Mon, 06 Jan 2003 19:55:49 GMT,

>> Hello, how could i take a long string in perl and insert
>> newlines at the whitespace nearest to 80 chars? So a
>> string that contains 320 chars would have roughly 5
>> newlines inserted into it.

> Text::Wrap is one way.

  Thanks, this works well. I'm also curious about
  a solution using regular exp.
Quote:

> hth
> t



Fri, 24 Jun 2005 21:17:51 GMT  
 insert newlines in a long string

Quote:
> > I think what you need exactly is that whatever be the whitespace just
> > before/closest to 80 character (column margin), it should be replaced by
> > \n...Right?

> Yes, that is correct.

I found this... http://www.pgts.com.au/download/scripts/fm
And I will try this problem with regexp later.


Fri, 24 Jun 2005 21:31:48 GMT  
 insert newlines in a long string

Quote:

> >>> On Mon, 06 Jan 2003 19:55:49 GMT,

> >> Hello, how could i take a long string in perl and insert
> >> newlines at the whitespace nearest to 80 chars? So a
> >> string that contains 320 chars would have roughly 5
> >> newlines inserted into it.

> > Text::Wrap is one way.

>   Thanks, this works well. I'm also curious about
>   a solution using regular exp.

You probably mean a substitution operation (of which a regex is only
one part).  This comes close:

    s/(.{1,79}) /$1\n/g

However, please stick with the module.  The simple substitution doesn't
provide a final linefeed, and it doesn't treat the case when there isn't
a blank within 80 characters (this could be amended).  There are probably
more weird cases it doesn't deal with.  The module is tried and tested.

Anno



Fri, 24 Jun 2005 23:17:55 GMT  
 insert newlines in a long string

Try using the Text::Format module, it has other nice features as well.


   into Another Monday evening. Another summary to                                      write.
                        Starting, as is becoming tediously predictable,                                 with perl6-internals.
                        Another JIT Discussion Toward the end of the                                    previous week,
Leopold To"tsch posted something                           about the latest round of changes
to the JIT                                      core. Daniel Grunblatt was concerned that the                            
current JIT wasn't doing the correct thing when                                 it came to hardware
register allocation and                                         wanted to remove a some conditional
logic. Leo                                      didn't agree at first, but became convinced and                          
aniel's requested change was applied.
                };

        $text = Text::Format->new ;

        $text->columns(80);

Quote:

> Hello, how could i take a long string in perl
> and insert newlines at the whitespace nearest
> to 80 chars? So a string that contains 320 chars
> would have roughly 5 newlines inserted into
> it.



Fri, 24 Jun 2005 23:13:05 GMT  
 insert newlines in a long string

Quote:

> Hello, how could i take a long string in perl
> and insert newlines at the whitespace nearest
> to 80 chars? So a string that contains 320 chars
> would have roughly 5 newlines inserted into
> it.

use Text::AutoFormat; # from CPAN

--
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);



Sat, 25 Jun 2005 00:33:58 GMT  
 insert newlines in a long string



: > Hello, how could i take a long string in perl
: > and insert newlines at the whitespace nearest
: > to 80 chars? So a string that contains 320 chars
: > would have roughly 5 newlines inserted into
: > it.
:
: I think what you need exactly is that whatever be the whitespace just
: before/closest to 80 character (column margin), it should be replaced by
: \n...Right?

What should happen when there is a substring of eighty or more
characters that is not broken by whitespace?

The "what if" cases will kill ya.  :)



Sat, 25 Jun 2005 00:30:42 GMT  
 insert newlines in a long string

Quote:

> Hello, how could i take a long string in perl
> and insert newlines at the whitespace nearest
> to 80 chars? So a string that contains 320 chars
> would have roughly 5 newlines inserted into
> it.

 Damn, the PPT-version of fmt isn't done yet... ;-D

 <URL: http://www.perl.com/language/ppt/src/fmt/index.html >

        /t
--
# Per scientiam ad libertatem! // Through knowledge towards freedom! #

    perl -e'print$_{$_} for sort%_=`lynx -source svanstrom.com/t`'



Sat, 25 Jun 2005 06:34:15 GMT  
 insert newlines in a long string
[snipped]

Quote:
> : I think what you need exactly is that whatever be the whitespace just
> : before/closest to 80 character (column margin), it should be replaced by
> : \n...Right?

> What should happen when there is a substring of eighty or more
> characters that is not broken by whitespace?

In such a case, we should break the 'long' word at 79 characters, put a
hyphen as the 80 character and continue processing it.

Any other solutions for this??
--
Perl is designed to give you several ways to do anything, so
consider picking the most readable one.
             -- Larry Wall in the perl man page



Sat, 25 Jun 2005 06:45:27 GMT  
 insert newlines in a long string

Quote:

> --
> Perl is designed to give you several ways to do anything, so
> consider picking the most readable one.
>              -- Larry Wall in the perl man page

                                    ^^^^
                                    ^^^^

That is in the perlstyle man page nowadays.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sat, 25 Jun 2005 07:01:51 GMT  
 insert newlines in a long string
[snip]
Quote:
> > --
> > Perl is designed to give you several ways to do anything, so
> > consider picking the most readable one.
> >              -- Larry Wall in the perl man page
>                                     ^^^^
>                                     ^^^^
> That is in the perlstyle man page nowadays.

[snip]

Hi Tad,
I got this from http://www.perl.com/CPAN/misc/lwall-quotes.txt.gz
and it says....
    %%
    Perl is designed to give you several ways to do anything, so
    consider picking the most readable one.
                 -- Larry Wall in the perl man page
    %%

but from www.google.com I found that it's also mentioned in perlstyle.

So my _new_ signature should be free from controversy now :-)
--
Perl is designed to give you several ways to do anything, so
consider picking the most readable one.
             -- Larry Wall



Sat, 25 Jun 2005 08:04:42 GMT  
 insert newlines in a long string

Quote:

> Hello, how could i take a long string in perl
> and insert newlines at the whitespace nearest
> to 80 chars? So a string that contains 320 chars
> would have roughly 5 newlines inserted into
> it.

Thanks for all the suggestions. Obviously Text::Wrap
is the way to go. Oddly enough, this problem is one of
the few where a homegrown solution would be easier to
accomplish in C where strings are arrays as opposed
to Perl scalars.


Sat, 25 Jun 2005 16:19:16 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. How to insert newline break into a long string

2. Insert newline into 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