wordwrap function

Discussion in 'Perl Misc' started by hudson, Jul 15, 2004.

  1. hudson

    hudson Guest

    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?
     
    hudson, Jul 15, 2004
    #1
    1. Advertising

  2. hudson

    Matt Garrish Guest

    "hudson" <> wrote in message
    news:...
    >
    > 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
     
    Matt Garrish, Jul 15, 2004
    #2
    1. Advertising

  3. hudson

    Matt Garrish Guest

    "hudson" <> wrote in message
    news:...
    > 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
     
    Matt Garrish, Jul 15, 2004
    #3
  4. hudson

    hudson Guest

    On Wed, 14 Jul 2004 22:40:44 -0400, "Matt Garrish"
    <> wrote:

    >
    >"hudson" <> wrote in message
    >news:...
    >> 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
     
    hudson, Jul 15, 2004
    #4
  5. hudson

    Jay Tilton Guest

    hudson <> wrote:

    : Here's my idea for a wordwrap function:

    Is there something you dislike about the Text::Wrap module?
     
    Jay Tilton, Jul 16, 2004
    #5
  6. hudson

    hudson Guest

    On Fri, 16 Jul 2004 00:34:21 GMT, (Jay Tilton) wrote:

    >hudson <> 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 ;-)
     
    hudson, Jul 17, 2004
    #6
  7. hudson

    hudson Guest

    On Wed, 14 Jul 2004 22:25:31 -0400, "Matt Garrish" <> wrote:

    >
    >"hudson" <> wrote in message
    >news:...
    >>
    >> 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
     
    hudson, Jul 17, 2004
    #7
  8. hudson <> wrote:
    > On Fri, 16 Jul 2004 00:34:21 GMT, (Jay Tilton) wrote:
    >
    >>hudson <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 17, 2004
    #8
  9. hudson

    Jay Tilton Guest

    hudson <> wrote:

    : On Fri, 16 Jul 2004 00:34:21 GMT, (Jay Tilton) wrote:
    :
    : >hudson <> 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? :)
     
    Jay Tilton, Jul 17, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    Wordwrap DataGrid Columns

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Nov 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,830
    Saravana
    Nov 4, 2004
  2. Harvey Triana

    GridView without wordwrap

    Harvey Triana, Apr 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    532
    Harvey Triana
    Apr 3, 2006
  3. Matt
    Replies:
    1
    Views:
    522
    Sid Ismail
    Dec 23, 2004
  4. Christoph

    <pre> and wordwrap

    Christoph, Sep 23, 2006, in forum: HTML
    Replies:
    1
    Views:
    570
    Toby Inkster
    Sep 23, 2006
  5. =?Utf-8?B?Z3V5?=

    GridView forcing wordwrap

    =?Utf-8?B?Z3V5?=, Oct 29, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    577
    Cowboy \(Gregory A. Beamer\)
    Oct 29, 2007
Loading...

Share This Page