Hi Chris,
I had to do something similar I am producing a text newsletter I do not want my lines
of text in each paragraph to exceed 75 characters. Basically you take your string of
text and assign the full words to an array you can use the split function to do this.
So now you've got your array of words, you just loop through this constructing the
sentences to the required length then you output a carriage return '\n' or a <br>
depending on the display medium. Start over again until there are no more words left
in the array. Anyway take what you need from my code below.
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
#sample headings
@arrHeadings[0] = "School is in at the Australian National Field Days";
@arrHeadings[1] = "Wiggles' colourful splash to Minister's swim scheme launch";
@arrHeadings[2] = "Student's raise \$81,000 for Stewart House ";
#sample paragraphs
@arrParas[0] = "Public schools are adding a new dimension at Orange's Australian
National Field Days - long recognised as a leading educational resource for farmers.";
@arrParas[1] = "Popular international children's entertainers, The Wiggles, added a
splash of colour when the Minister for Education and Training, John Watkins, launched
the 2002 School Swimming Scheme.";
@arrParas[2] = "Public school students throughout NSW and the ACT have excelled in
their fundraising for the Stewart House charity this year, raising \$81,000 in the
annual Stewart House Donation Drive raffle.";
#initialise output string to "";
my $strOutput = "";
while( @arrHeadings ){
#assign the first element of array headings to $heading
my $heading = shift(@arrHeadings);
#I want to put a space between each paragraph
if($strOutput ne ""){
$strOutput .= "<br>";
}
#add the heading to the output string
$strOutput .= "\>\> $heading<br>";
#assign the first element of array paragraphs to $para
my $para = shift(@arrParas);
#test length of $para if less than 75 characters then I don't need
#to manipulate it, I just add it to the output string.
if(length($para)<=75){
$strOutput .= "$para<br>";
}
#because length of $para > 75 I need to wrap each line
#firstly I break each line up into its component words
elsif(length($para)>75){
my $line = "";
my @arrWords = split( / /, $para);
#loop wile elements in array words
while(@arrWords){
#get and remove first element of arrwords
$word = shift(@arrWords);
if((length($line)+length($word))<=75){
$line .= "$word ";
}
elsif((length($line)+length($word))>75){
$strOutput .= "$line<br>";
$line = "";
$line .= "$word ";
}
}
#I have jumped out of the loop but is their elements in $line that
#need to be added to #strOutput
if(length($line)>0){
$strOutput .= "$line<br>";
}
}
}
print "$strOutput";
It worked for me, I hope it works for you.
Colin Johnstone
Website Project Officer
Corporate Website Unit
NSW Department of Education And Training
AUSTRALIA
-----Original Message-----
From: chris [mailto:chris@;home.com]
Sent: Saturday, October 19, 2002 11:43
To: [EMAIL PROTECTED]
Subject: How to wrap a sentence?
Given a lengthy string of words, I need to limit the line length and
wrap it into multiple lines by introducing line breaks. Do not care
about sentence or paragraph format, just respect full words.
How do I do this?
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]