Perl text-handling help

C

Camelback Jones

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.
 
P

Paul Lalli

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
 
P

Paul Lalli

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;
 
C

Camelback Jones

Paul said:
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...";
 
B

Ben Morrow

Quoth Camelback Jones said:
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
 
C

Camelback Jones

Ben said:
Quoth Camelback Jones said:
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.
 
C

Camelback Jones

Camelback said:
Ben said:
Quoth Camelback Jones said:
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.
 
P

Paul Lalli

Ben said:
Quoth Camelback Jones said:
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
 
C

Camelback Jones

Paul said:
Ben said:
Quoth Camelback Jones <[email protected]>:

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";
}
 
B

Bob Walton

Camelback said:
Ben Morrow 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";
 
A

Anno Siegel

Camelback Jones said:
Paul said:
Ben Morrow wrote:

Quoth Camelback Jones <[email protected]>:

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,269
Messages
2,571,100
Members
48,773
Latest member
Kaybee

Latest Threads

Top