Tricky column formatting/space removing question 
Author Message
 Tricky column formatting/space removing question

I'm parsing data inputted via an html form.  $body=multiline textual data of
arbitrary length.   I want to preserve the paragraph separations, while
making the lines themselves 80 columns in length.  At the same time, I am
contending with white space at the beginning of each line, of abitrary lenth.

Here's what I've kludged together so far:

$body =~ s/\n\s*/\n/g;  ## Get rid of multiple spaces that follow a new line.
$body =~ s/^\s*//g;  ## Gets rid of the initial space missed by prev regex

(That ain't pretty, but it works... anyone got a better way??)

new Text::Format->columns(80);   # Set Column width to 80
$body=new Text::Format->format($body);  # Standardize paragraphs to 80 chars

The problem is, this concatenates all of the pre-existing paragraphs into a
single large uber paragraph.  

How can I fold the long lines, merge the short lines, but preserve the extra
CR/LF's between paragraphs?

Any help appreciate...

--
.............................................................................

"The Israeli objective since 1967 has been to transform the ethnic character
 of the annexed area [of East Jerusalem] from Arab to Jewish. Israeli
 demolition policy in East Jerusalem is based on similar premises as in the
 rest of the West Bank: construction of settlements restricted to Jews,
 confiscation of land for exclusively Jewish use, and restriction and
 demolition of Palestinian development."

                                       -Amnesty International Report:
                                        "The Destruction of Palestinian Homes"
                                        December 8, 1999.

.............................................................................



Tue, 18 May 2004 06:36:38 GMT  
 Tricky column formatting/space removing question

Quote:

> I'm parsing data inputted via an html form.  

I take it you used CGI for that ;)

Quote:
> $body=multiline textual data of arbitrary length.  I want to preserve
> the paragraph separations, while making the lines themselves 80
> columns in length.  At the same time, I am contending with white space
> at the beginning of each line, of abitrary lenth.

Call me old-fashioned, but I use perl formats for that.  Grab the
swrite() sub from the "perlform" manpage, and try something like

  use constant NUM_COLS => 80;
  sub swrite; # see perlform

  my $fmt = "^" . ("<" x (NUM_COLS - 1) ) . "~~\n";


<ot>
  btw- how's the colsfaq coming along?
</ot>
--
"[Perl] transcended implementation differences by staring them in the eye and
fighting it out, not by giving up, whining that something isn't standard and
portable, etc. It gave the bad standards and their nonsensical implementation
differences the finger and wrote its own standard."
                                              -- Erik Naggum in comp.lang.lisp



Tue, 18 May 2004 07:47:32 GMT  
 Tricky column formatting/space removing question

Quote:

>   use constant NUM_COLS => 80;
>   sub swrite; # see perlform

Sorry, I omitted an important detail:

    $body =~ s/^\s+//; # drop leading space
    $body =~ s/\s+$//; #  "   trailing space

(See the recent FAQ posting in a parallel thread.) The split()
below should take care of this issue for the middle paragraphs.

Quote:
>   my $fmt = "^" . ("<" x (NUM_COLS - 1) ) . "~~\n";


HTH
--
Joe Schaefer   "A superior algorithm trumps superior code.  Every Single Time.
                                           Almost."
                                               -- Eric Sosman in comp.lang.c


Tue, 18 May 2004 08:38:28 GMT  
 Tricky column formatting/space removing question

Quote:

>   use constant NUM_COLS => 80;
>   sub swrite; # see perlform
>   my $fmt = "^" . ("<" x (NUM_COLS - 1) ) . "~~\n";



Joe, I lack the skill to visually appreciate your suggestion, but am going
to plug it in, and try it out.  

Quote:
> <ot>
>   btw- how's the colsfaq coming along?
> </ot>

It's coming along, but never well.  I regularly find myself distracted by shiny
objects, at the expense of those I already posess which may require polishing.

--
............................................................................

     Even if you do learn to speak correct English, whom are you going
     to speak  it to?      
                                            -Clarence Darrow    

............................................................................



Tue, 18 May 2004 18:08:26 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. SUMMARY: Tricky pattern matching question - (at least I think it's tricky)

2. Tricky pattern matching question - (at least I think it's tricky)

3. **FORMAT Formatting columns/pages

4. Extra spaces in output and concatenating columns

5. DBD::CSV with column names containing spaces

6. Howto Parse data in spaced columns???

7. how to remove duplines by first column?

8. Tk::Table removing cells, rows, columns

9. Columns from a format definition?

10. Multi-column format?

11. Formatting output in columns:

12. Format a one line file to get two columns

 

 
Powered by phpBB® Forum Software