Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > wordwrap function

Reply
Thread Tools

wordwrap function

 
 
hudson
Guest
Posts: n/a
 
      07-15-2004
Here's my idea for a wordwrap function:

Usage:
use lib "./";
use Modules::AddNewLines;
$output = add_new_lines($output);

Module:
---------------------------------------
package Modules::AddNewLines;
use strict;
use vars qw(@ISA @EXPORT $VERSION);

use Exporter;
$VERSION = 1.00; # Or higher
@ISA = qw(Exporter);

@EXPORT = qw(add_new_lines);

sub add_new_lines {
my $string = shift;
my $count = 1;
my $new_string;
while ($string =~ /(.)/g) {
$new_string .= $1;
if ($count >= 80 && $1 eq " ") {
$new_string .= "\n";
$count = 0;
}
$count++;
}
return $new_string;
}

1;
---------------------------------------

Well, I thought it was neat, but maybe someone has a few suggestions?
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      07-15-2004

"hudson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Well, I thought it was neat, but maybe someone has a few suggestions?
>


Your threshold for neatness is set too low.

Nothing to do while high school is out, or is there a reason why you want to
add newlines to files?

Matt


 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      07-15-2004

"hudson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Here's my idea for a wordwrap function:
>
> Usage:
> use lib "./";
> use Modules::AddNewLines;
> $output = add_new_lines($output);
>
> Module:
> ---------------------------------------
> package Modules::AddNewLines;
> use strict;
> use vars qw(@ISA @EXPORT $VERSION);
>
> use Exporter;
> $VERSION = 1.00; # Or higher
> @ISA = qw(Exporter);
>
> @EXPORT = qw(add_new_lines);
>
> sub add_new_lines {
> my $string = shift;
> my $count = 1;
> my $new_string;
> while ($string =~ /(.)/g) {
> $new_string .= $1;
> if ($count >= 80 && $1 eq " ") {
> $new_string .= "\n";
> $count = 0;
> }
> $count++;
> }


One big problem with this code is that you aren't checking for existing
newlines. What if $string contains the entire contents of a slurped in file?
What if the paragraphs have already been broken up, but the user wants them
recombined and the line breaks to be standardized? What if the user wants
the line breaks at a different point?

The code has a very limited application as it's currently written. You need
to consider all the possibilities and write your code to accomodate them as
much as possible. Even if it's only for your own use, do you want to have to
go and modify the file each time you want to change the break length, or
would you rather be able to just specify an option in your code to set it
each time you call the subroutine?

Matt


 
Reply With Quote
 
hudson
Guest
Posts: n/a
 
      07-15-2004
On Wed, 14 Jul 2004 22:40:44 -0400, "Matt Garrish"
<(E-Mail Removed)> wrote:

>
>"hudson" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> Here's my idea for a wordwrap function:
>>
>> Usage:
>> use lib "./";
>> use Modules::AddNewLines;
>> $output = add_new_lines($output);
>>
>> Module:
>> ---------------------------------------
>> package Modules::AddNewLines;
>> use strict;
>> use vars qw(@ISA @EXPORT $VERSION);
>>
>> use Exporter;
>> $VERSION = 1.00; # Or higher
>> @ISA = qw(Exporter);
>>
>> @EXPORT = qw(add_new_lines);
>>
>> sub add_new_lines {
>> my $string = shift;
>> my $count = 1;
>> my $new_string;
>> while ($string =~ /(.)/g) {
>> $new_string .= $1;
>> if ($count >= 80 && $1 eq " ") {
>> $new_string .= "\n";
>> $count = 0;
>> }
>> $count++;
>> }

>
>One big problem with this code is that you aren't checking for existing
>newlines. What if $string contains the entire contents of a slurped in file?
>What if the paragraphs have already been broken up, but the user wants them
>recombined and the line breaks to be standardized? What if the user wants
>the line breaks at a different point?
>
>The code has a very limited application as it's currently written. You need
>to consider all the possibilities and write your code to accomodate them as
>much as possible. Even if it's only for your own use, do you want to have to
>go and modify the file each time you want to change the break length, or
>would you rather be able to just specify an option in your code to set it
>each time you call the subroutine?
>
>Matt
>


well, I never thought of all that! thanks...I find perl really forces
you to generalize and I am trying, but I guess I need to generalize a
bit more
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      07-16-2004
hudson <(E-Mail Removed)> wrote:

: Here's my idea for a wordwrap function:

Is there something you dislike about the Text::Wrap module?

 
Reply With Quote
 
hudson
Guest
Posts: n/a
 
      07-17-2004
On Fri, 16 Jul 2004 00:34:21 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed) (Jay Tilton) wrote:

>hudson <(E-Mail Removed)> wrote:
>
>: Here's my idea for a wordwrap function:
>
>Is there something you dislike about the Text::Wrap module?


I tried it once and it seemed very slow...but I might have been
mistaked or tried to run some super huge file through it

 
Reply With Quote
 
hudson
Guest
Posts: n/a
 
      07-17-2004
On Wed, 14 Jul 2004 22:25:31 -0400, "Matt Garrish" <(E-Mail Removed)> wrote:

>
>"hudson" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>>
>> Well, I thought it was neat, but maybe someone has a few suggestions?
>>

>
>Your threshold for neatness is set too low.
>

actually, it was in combination with a few things....storing a sub routine
in a hash and combining that with print...so being able to do:

print format_string($config{action}(split /\n/, $kv{words}));

seemed really neat to me...but sorry, I didn't really get that across in
my orignial post
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-17-2004
hudson <(E-Mail Removed)> wrote:
> On Fri, 16 Jul 2004 00:34:21 GMT, (E-Mail Removed) (Jay Tilton) wrote:
>
>>hudson <(E-Mail Removed)> wrote:
>>
>>: Here's my idea for a wordwrap function:
>>
>>Is there something you dislike about the Text::Wrap module?

>
> I tried it once and it seemed very slow...



Slow and correct is better than fast and wrong.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      07-17-2004
hudson <(E-Mail Removed)> wrote:

: On Fri, 16 Jul 2004 00:34:21 GMT, (E-Mail Removed) (Jay Tilton) wrote:
:
: >hudson <(E-Mail Removed)> wrote:
: >
: >: Here's my idea for a wordwrap function:
: >
: >Is there something you dislike about the Text::Wrap module?
:
: I tried it once and it seemed very slow...but I might have been
: mistaked or tried to run some super huge file through it

The Benchmark module can quantify speed differences. You will probably see
an insignificant difference between the two subs, even with the Text::Wrap
module's extra whizbangs.

Using s/// can provide results similar to your sub in a fraction of the
time.

sub regex_wrap {
(my $o = shift) =~ s/(.{79,}? )/$1\n/g;
$o;
}

Embedded linebreaks screw up both solutions, but in different ways. Is it
important to get a bad result as quickly as possible?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView forcing wordwrap =?Utf-8?B?Z3V5?= ASP .Net 1 10-29-2007 03:18 PM
<pre> and wordwrap Christoph HTML 1 09-23-2006 08:26 PM
GridView without wordwrap Harvey Triana ASP .Net 0 04-03-2006 03:12 PM
table and wordwrap formatting Matt HTML 1 12-23-2004 07:01 AM
Wordwrap DataGrid Columns =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?= ASP .Net 1 11-04-2004 04:41 AM



Advertisments