Perl text-handling help

Discussion in 'Perl Misc' started by Camelback Jones, Jun 11, 2004.

  1. In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is a set of
    functions for handling "memo" files of text. Basically, a memo is a chunk
    of text of any size whatever, and the functions include the ability to set
    the desired width of output text, find the number of lines of text in the
    memo (given the desired width), and get any given line of text (not simply
    cutting it off, but respecting word divisions including punctuation - that
    is, it might end a line on a ["] but not if it were [",]). This, as you
    can understand, is very handy for displaying or printing stored text in a
    tabular format with whatever width might be required.

    I'm assuming (always dangerous) that a corresponding set of code is
    available somewhere in the perl universe, but I haven't come across it
    yet...

    While I continue to search, if anyone knows of such code, I'd appreciate a
    specific link to it.


    --
    The greatest unsolved problem in mathematics is why some people are
    better at it than others.
    Camelback Jones, Jun 11, 2004
    #1
    1. Advertising

  2. Camelback Jones

    Paul Lalli Guest

    On Fri, 11 Jun 2004, Camelback Jones wrote:

    > In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is a set of
    > functions for handling "memo" files of text. Basically, a memo is a chunk
    > of text of any size whatever, and the functions include the ability to set
    > the desired width of output text, find the number of lines of text in the
    > memo (given the desired width), and get any given line of text (not simply
    > cutting it off, but respecting word divisions including punctuation - that
    > is, it might end a line on a ["] but not if it were [",]). This, as you
    > can understand, is very handy for displaying or printing stored text in a
    > tabular format with whatever width might be required.
    >
    > I'm assuming (always dangerous) that a corresponding set of code is
    > available somewhere in the perl universe, but I haven't come across it
    > yet...
    >
    > While I continue to search, if anyone knows of such code, I'd appreciate a
    > specific link to it.


    I haven't played with them very much, but Text::Wrap or Text::Format might
    be what you're looking for. I *think* Text::Wrap is standard, so just do
    perldoc Text::Wrap
    whereas you can find documentation for Text::Format by searching at
    http://search.cpan.org

    Paul Lalli
    Paul Lalli, Jun 11, 2004
    #2
    1. Advertising

  3. Camelback Jones

    Paul Lalli Guest

    On Fri, 11 Jun 2004, Paul Lalli wrote:

    > On Fri, 11 Jun 2004, Camelback Jones wrote:
    >
    > > In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is a set of
    > > functions for handling "memo" files of text. Basically, a memo is a chunk
    > > of text of any size whatever, and the functions include the ability to set
    > > the desired width of output text, find the number of lines of text in the
    > > memo (given the desired width), and get any given line of text (not simply
    > > cutting it off, but respecting word divisions including punctuation - that
    > > is, it might end a line on a ["] but not if it were [",]). This, as you
    > > can understand, is very handy for displaying or printing stored text in a
    > > tabular format with whatever width might be required.
    > >
    > > I'm assuming (always dangerous) that a corresponding set of code is
    > > available somewhere in the perl universe, but I haven't come across it
    > > yet...
    > >
    > > While I continue to search, if anyone knows of such code, I'd appreciate a
    > > specific link to it.

    >


    my $mypost = <<POST;

    > I haven't played with them very much, but Text::Wrap or Text::Format might
    > be what you're looking for. I *think* Text::Wrap is standard, so just do
    > perldoc Text::Wrap
    > whereas you can find documentation for Text::Format by searching at
    > http://search.cpan.org
    >
    > Paul Lalli
    >


    POST

    $mypost =~ s/Format/Autoformat/g;

    print $mypost;
    Paul Lalli, Jun 11, 2004
    #3
  4. Paul Lalli wrote:

    > On Fri, 11 Jun 2004, Paul Lalli wrote:
    >
    >> On Fri, 11 Jun 2004, Camelback Jones wrote:
    >>
    >> > In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is a set
    >> > of functions for handling "memo" files of text. Basically, a memo is a
    >> > chunk of text of any size whatever, and the functions include the
    >> > ability to set
    >> > the desired width of output text, find the number of lines of text in
    >> > the memo (given the desired width), and get any given line of text (not
    >> > simply cutting it off, but respecting word divisions including
    >> > punctuation - that
    >> > is, it might end a line on a ["] but not if it were [",]). This, as
    >> > you can understand, is very handy for displaying or printing stored
    >> > text in a tabular format with whatever width might be required.
    >> >
    >> > I'm assuming (always dangerous) that a corresponding set of code is
    >> > available somewhere in the perl universe, but I haven't come across it
    >> > yet...
    >> >
    >> > While I continue to search, if anyone knows of such code, I'd
    >> > appreciate a specific link to it.

    >>

    >
    > my $mypost = <<POST;
    >
    >> I haven't played with them very much, but Text::Wrap or Text::Format
    >> might
    >> be what you're looking for. I *think* Text::Wrap is standard, so just do
    >> perldoc Text::Wrap
    >> whereas you can find documentation for Text::Format by searching at
    >> http://search.cpan.org
    >>
    >> Paul Lalli
    >>

    >
    > POST
    >
    > $mypost =~ s/Format/Autoformat/g;
    >
    > print $mypost;



    ---------------- P R O G R E S S ---------------------

    So far, so good... thanks, Paul!!

    I've not looked for ::Format or ::Autoformat yet - maybe they're the answer
    to the remaining...

    Text::Wrap formats the text for output nicely, but leaves a bit to be
    desired with regard to being able to retrieve a specific line. The problem
    is that the formatted output is not consistent in length, so that direct
    access by offset doesn't work reliably... I'm looking for a way to put the
    output text in an array (list?) so that a desired line is found simply by
    the array index.

    use Text::Wrap;
    $linelen = 53;
    Text::Wrap::columns = $linelen;

    $text = magic();

    print wrap('','',$text);
    print "\n\nso far, so good...";


    --
    The greatest unsolved problem in mathematics is why some people are
    better at it than others.
    Camelback Jones, Jun 11, 2004
    #4
  5. Camelback Jones

    Ben Morrow Guest

    Quoth Camelback Jones <>:
    > Paul Lalli wrote:
    >
    > > On Fri, 11 Jun 2004, Paul Lalli wrote:
    > >
    > >> On Fri, 11 Jun 2004, Camelback Jones wrote:
    > >>
    > >> > In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is a set
    > >> > of functions for handling "memo" files of text. Basically, a memo is a
    > >> > chunk of text of any size whatever, and the functions include the
    > >> > ability to set
    > >> > the desired width of output text, find the number of lines of text in
    > >> > the memo (given the desired width), and get any given line of text

    >
    > Text::Wrap formats the text for output nicely, but leaves a bit to be
    > desired with regard to being able to retrieve a specific line. The problem
    > is that the formatted output is not consistent in length, so that direct
    > access by offset doesn't work reliably... I'm looking for a way to put the
    > output text in an array (list?) so that a desired line is found simply by
    > the array index.
    >
    > use Text::Wrap;
    > $linelen = 53;
    > Text::Wrap::columns = $linelen;
    >
    > $text = magic();
    >
    > print wrap('','',$text);
    > print "\n\nso far, so good...";


    my @wrapped = wrap '', '', $text;
    print "line 6 is $wrapped[5]\n";

    Ben

    --
    I've seen things you people wouldn't believe: attack ships on fire off
    the shoulder of Orion; I watched C-beams glitter in the dark near the
    Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
    Time to die.
    Ben Morrow, Jun 11, 2004
    #5
  6. Ben Morrow wrote:

    >
    > Quoth Camelback Jones <>:
    >> Paul Lalli wrote:
    >>
    >> > On Fri, 11 Jun 2004, Paul Lalli wrote:
    >> >
    >> >> On Fri, 11 Jun 2004, Camelback Jones wrote:
    >> >>
    >> >> > In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is a
    >> >> > set of functions for handling "memo" files of text. Basically, a
    >> >> > memo is a chunk of text of any size whatever, and the functions
    >> >> > include the ability to set
    >> >> > the desired width of output text, find the number of lines of text
    >> >> > in the memo (given the desired width), and get any given line of
    >> >> > text

    >>
    >> Text::Wrap formats the text for output nicely, but leaves a bit to be
    >> desired with regard to being able to retrieve a specific line. The
    >> problem is that the formatted output is not consistent in length, so that
    >> direct access by offset doesn't work reliably... I'm looking for a way to
    >> put the output text in an array (list?) so that a desired line is found
    >> simply by the array index.
    >>
    >> use Text::Wrap;
    >> $linelen = 53;
    >> Text::Wrap::columns = $linelen;
    >>
    >> $text = magic();
    >>
    >> print wrap('','',$text);
    >> print "\n\nso far, so good...";

    >
    > my @wrapped = wrap '', '', $text;
    > print "line 6 is $wrapped[5]\n";
    >
    > Ben


    Thanks, Ben, but it didn't work - came out empty ($text is about 12 lines
    long at 50 char per line, so line 6 does exist).

    print"12345678901234567890123456789012345678901234567890\n";
    print wrap('','',$text);
    print "\n\nThat was it...\n\n"; ## correct output
    <STDIN>;
    print "Now... about those individual lines...\n\n";
    my @$wrapped = wrap '','',$text;
    print "line 6 is $wrapped[5]\n";
    print "... isn't it?\n"; ## nope - it was blank, empty, null... whatever.
    <STDIN>;



    However, plain old Text::Format does provide a rightFill() that creates
    contant-length lines, so that'll work.


    --
    The greatest unsolved probem in mathematics is why some people are
    better at it than others.
    Camelback Jones, Jun 11, 2004
    #6
  7. Camelback Jones wrote:

    > Ben Morrow wrote:
    >
    >>
    >> Quoth Camelback Jones <>:
    >>> Paul Lalli wrote:
    >>>
    >>> > On Fri, 11 Jun 2004, Paul Lalli wrote:
    >>> >
    >>> >> On Fri, 11 Jun 2004, Camelback Jones wrote:
    >>> >>
    >>> >> > In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is a
    >>> >> > set of functions for handling "memo" files of text. Basically, a
    >>> >> > memo is a chunk of text of any size whatever, and the functions
    >>> >> > include the ability to set
    >>> >> > the desired width of output text, find the number of lines of text
    >>> >> > in the memo (given the desired width), and get any given line of
    >>> >> > text
    >>>
    >>> Text::Wrap formats the text for output nicely, but leaves a bit to be
    >>> desired with regard to being able to retrieve a specific line. The
    >>> problem is that the formatted output is not consistent in length, so
    >>> that direct access by offset doesn't work reliably... I'm looking for a
    >>> way to put the output text in an array (list?) so that a desired line is
    >>> found simply by the array index.
    >>>
    >>> use Text::Wrap;
    >>> $linelen = 53;
    >>> Text::Wrap::columns = $linelen;
    >>>
    >>> $text = magic();
    >>>
    >>> print wrap('','',$text);
    >>> print "\n\nso far, so good...";

    >>
    >> my @wrapped = wrap '', '', $text;
    >> print "line 6 is $wrapped[5]\n";
    >>
    >> Ben

    >
    > Thanks, Ben, but it didn't work - came out empty ($text is about 12 lines
    > long at 50 char per line, so line 6 does exist).
    >
    > print"12345678901234567890123456789012345678901234567890\n";
    > print wrap('','',$text);
    > print "\n\nThat was it...\n\n"; ## correct output
    > <STDIN>;
    > print "Now... about those individual lines...\n\n";
    > my @$wrapped = wrap '','',$text;
    > print "line 6 is $wrapped[5]\n";
    > print "... isn't it?\n"; ## nope - it was blank, empty, null... whatever.
    > <STDIN>;
    >
    >
    >
    > However, plain old Text::Format does provide a rightFill() that creates
    > contant-length lines, so that'll work.
    >
    >



    By the way - I pasted this line from the working program

    > my @$wrapped = wrap '','',$text;


    and this was after trying various combinations with and without the $ and
    the @ and in different orders... maybe I just skipped the one that really
    works... but I know that

    > my @wrapped = wrap '','',$text;


    didn't work.

    --
    The greatest unsolved probem in mathematics is why some people are
    better at it than others.
    Camelback Jones, Jun 11, 2004
    #7
  8. Camelback Jones

    Paul Lalli Guest

    On Fri, 11 Jun 2004, Camelback Jones wrote:

    > Ben Morrow wrote:
    >
    > > Quoth Camelback Jones <>:
    > >
    > >> Paul Lalli wrote:
    > >>
    > >> > On Fri, 11 Jun 2004, Paul Lalli wrote:
    > >> >
    > >> >> On Fri, 11 Jun 2004, Camelback Jones wrote:
    > >> >>
    > >> >> > In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is a
    > >> >> > set of functions for handling "memo" files of text. Basically, a
    > >> >> > memo is a chunk of text of any size whatever, and the functions
    > >> >> > include the ability to set
    > >> >> > the desired width of output text, find the number of lines of text
    > >> >> > in the memo (given the desired width), and get any given line of
    > >> >> > text
    > >>
    > >> Text::Wrap formats the text for output nicely, but leaves a bit to be
    > >> desired with regard to being able to retrieve a specific line. The
    > >> problem is that the formatted output is not consistent in length, so that
    > >> direct access by offset doesn't work reliably... I'm looking for a way to
    > >> put the output text in an array (list?) so that a desired line is found
    > >> simply by the array index.
    > >>
    > >> use Text::Wrap;
    > >> $linelen = 53;
    > >> Text::Wrap::columns = $linelen;
    > >>
    > >> $text = magic();
    > >>
    > >> print wrap('','',$text);
    > >> print "\n\nso far, so good...";

    > >
    > > my @wrapped = wrap '', '', $text;
    > > print "line 6 is $wrapped[5]\n";

    >
    > Thanks, Ben, but it didn't work - came out empty ($text is about 12 lines
    > long at 50 char per line, so line 6 does exist).


    Like I said in my original post, I hadn't played with this module very
    often. I did just run a few tests, however. It seems like wrap() always
    returns the formatted data as scalar. I don't know why the documentation
    shows the return value being assigned to an array. That being the case,
    if you want to find the 6th line, there are a variety of ways you could do
    it. Here's one. I'm willing to bet others will suggest more.

    my $formatted = wrap('','', @orig);
    my ($line) = $formatted =~ m|(?:.*$/){5}(.*)|;

    (Note that I used $/ in the pattern instead of just \n just in case you've
    decided to change your output record seperator, or in case your OS uses a
    different definition. This may or may not be necessary for your
    situation).

    Paul Lalli
    Paul Lalli, Jun 12, 2004
    #8
  9. Paul Lalli wrote:

    > On Fri, 11 Jun 2004, Camelback Jones wrote:
    >
    >> Ben Morrow wrote:
    >>
    >> > Quoth Camelback Jones <>:
    >> >
    >> >> Paul Lalli wrote:
    >> >>
    >> >> > On Fri, 11 Jun 2004, Paul Lalli wrote:
    >> >> >
    >> >> >> On Fri, 11 Jun 2004, Camelback Jones wrote:
    >> >> >>
    >> >> >> > In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is
    >> >> >> > a set of functions for handling "memo" files of text. Basically,
    >> >> >> > a memo is a chunk of text of any size whatever, and the functions
    >> >> >> > include the ability to set
    >> >> >> > the desired width of output text, find the number of lines of
    >> >> >> > text in the memo (given the desired width), and get any given
    >> >> >> > line of text
    >> >>
    >> >> Text::Wrap formats the text for output nicely, but leaves a bit to be
    >> >> desired with regard to being able to retrieve a specific line. The
    >> >> problem is that the formatted output is not consistent in length, so
    >> >> that direct access by offset doesn't work reliably... I'm looking for
    >> >> a way to put the output text in an array (list?) so that a desired
    >> >> line is found simply by the array index.
    >> >>
    >> >> use Text::Wrap;
    >> >> $linelen = 53;
    >> >> Text::Wrap::columns = $linelen;
    >> >>
    >> >> $text = magic();
    >> >>
    >> >> print wrap('','',$text);
    >> >> print "\n\nso far, so good...";
    >> >
    >> > my @wrapped = wrap '', '', $text;
    >> > print "line 6 is $wrapped[5]\n";

    >>
    >> Thanks, Ben, but it didn't work - came out empty ($text is about 12 lines
    >> long at 50 char per line, so line 6 does exist).

    >
    > Like I said in my original post, I hadn't played with this module very
    > often. I did just run a few tests, however. It seems like wrap() always
    > returns the formatted data as scalar. I don't know why the documentation
    > shows the return value being assigned to an array. That being the case,
    > if you want to find the 6th line, there are a variety of ways you could do
    > it. Here's one. I'm willing to bet others will suggest more.
    >
    > my $formatted = wrap('','', @orig);
    > my ($line) = $formatted =~ m|(?:.*$/){5}(.*)|;
    >
    > (Note that I used $/ in the pattern instead of just \n just in case you've
    > decided to change your output record seperator, or in case your OS uses a
    > different definition. This may or may not be necessary for your
    > situation).
    >
    > Paul Lalli






    Ah, well... here's an ugly little snippet that works - I'm fixin' to turn
    it into a subroutine to return the nlineth bit of text

    # and what about plain ol' format???
    $fext = Text::Format->new();
    $fext->firstIndent(0); # no indent first line
    $fext->columns($textlen); #break into give length
    $fext->rightFill(1); # right pad with blanks
    $lines=$fext->format($text); # git it

    print "\n\n\n";
    print " 123456789012345678901234567890123456789012345678901234567890\n";
    # pull out the first ten or so lines...
    for ($nline=1;$nline<10;$nline++)
    {
    # extract the nlineth bit of text
    $start = ($textlen+1)*($nline-1);
    $newline = substr($lines, $start, $textlen);
    print "\"$newline\" was line $nline\n\n";
    }





    --
    The greatest unsolved probem in mathematics is why some people are
    better at it than others.
    Camelback Jones, Jun 12, 2004
    #9
  10. Camelback Jones

    Bob Walton Guest

    Camelback Jones wrote:

    > Ben Morrow wrote:
    >
    >
    >>Quoth Camelback Jones <>:
    >>
    >>>Paul Lalli wrote:
    >>>
    >>>
    >>>>On Fri, 11 Jun 2004, Paul Lalli wrote:
    >>>>
    >>>>
    >>>>>On Fri, 11 Jun 2004, Camelback Jones wrote:


    > my @$wrapped = wrap '','',$text;
    > print "line 6 is $wrapped[5]\n";



    If you're going to use $wrapped as an array reference, you have to be
    consistent -- the print should be:

    print "line 6 is $$wrapped[5]\n";

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Jun 12, 2004
    #10
  11. Camelback Jones

    Anno Siegel Guest

    Camelback Jones <> wrote in comp.lang.perl.misc:
    > Paul Lalli wrote:
    >
    > > On Fri, 11 Jun 2004, Camelback Jones wrote:
    > >
    > >> Ben Morrow wrote:
    > >>
    > >> > Quoth Camelback Jones <>:
    > >> >
    > >> >> Paul Lalli wrote:
    > >> >>
    > >> >> > On Fri, 11 Jun 2004, Paul Lalli wrote:
    > >> >> >
    > >> >> >> On Fri, 11 Jun 2004, Camelback Jones wrote:
    > >> >> >>
    > >> >> >> > In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is
    > >> >> >> > a set of functions for handling "memo" files of text. Basically,
    > >> >> >> > a memo is a chunk of text of any size whatever, and the functions
    > >> >> >> > include the ability to set
    > >> >> >> > the desired width of output text, find the number of lines of
    > >> >> >> > text in the memo (given the desired width), and get any given
    > >> >> >> > line of text
    > >> >>
    > >> >> Text::Wrap formats the text for output nicely, but leaves a bit to be
    > >> >> desired with regard to being able to retrieve a specific line. The
    > >> >> problem is that the formatted output is not consistent in length, so
    > >> >> that direct access by offset doesn't work reliably... I'm looking for
    > >> >> a way to put the output text in an array (list?) so that a desired
    > >> >> line is found simply by the array index.
    > >> >>
    > >> >> use Text::Wrap;
    > >> >> $linelen = 53;
    > >> >> Text::Wrap::columns = $linelen;
    > >> >>
    > >> >> $text = magic();
    > >> >>
    > >> >> print wrap('','',$text);
    > >> >> print "\n\nso far, so good...";
    > >> >
    > >> > my @wrapped = wrap '', '', $text;
    > >> > print "line 6 is $wrapped[5]\n";
    > >>
    > >> Thanks, Ben, but it didn't work - came out empty ($text is about 12 lines
    > >> long at 50 char per line, so line 6 does exist).

    > >
    > > Like I said in my original post, I hadn't played with this module very
    > > often. I did just run a few tests, however. It seems like wrap() always
    > > returns the formatted data as scalar. I don't know why the documentation
    > > shows the return value being assigned to an array. That being the case,
    > > if you want to find the 6th line, there are a variety of ways you could do
    > > it. Here's one. I'm willing to bet others will suggest more.
    > >
    > > my $formatted = wrap('','', @orig);
    > > my ($line) = $formatted =~ m|(?:.*$/){5}(.*)|;


    Well, split suggests itself to just supply the missing functionality
    to Text::Wrap. To make it work as advertised, I'd do:

    use Text::Wrap();
    sub wrap {
    return wantarray ? split /\n/, $_ : $_ for Text::Wrap::wrap( @_);
    }

    Then use the solutions that have been suggested, but rely on the behavior
    in array context.

    Anno
    Anno Siegel, Jun 12, 2004
    #11
    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. Robert V
    Replies:
    1
    Views:
    1,174
    Jeremy Fluhmann
    Jul 21, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,784
  3. Mark Tarver
    Replies:
    22
    Views:
    1,298
    J Kenneth King
    Apr 26, 2009
  4. Peter
    Replies:
    34
    Views:
    1,932
    James Kanze
    Oct 17, 2009
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    181
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page