HTML Paragraphs from Text

P

poopdeville

Hello,

I'm working on a project, and I'm having a little bit of trouble.
Basically, what I'm trying to do is take some text as input, split
along groups of newlines (so that multiple newlines in a row have the
same effect as a single one), and make each of the elements of the new
array an HTML paragraph. Here is the relevant subroutine:

#!/usr/bin/perl -w
#use strict;

#sub paragrapher {
# my @paragraphs = split /\n+/, $_[0];
# my $new;
# for (@paragraphs) {
# if (length($_)) {
# $new = $new . "<p>$_<\/p>";
# }
# }
# return $new;
#}

If we feed the subroutine the text:

This is the super dooper intro.

OMG, this is more of a test than an intro, but whatever.

stored in a variable, the output is

<p>This is the super dooper intro.</p><p>
</p><p>
OMG, this is more of a test than an intro, but whatever.</p>

Note the totally unnecessary <p></p>. Does anyone see what could be
going wrong?

Thanks,
'cid 'ooh
 
U

usenet

Note the totally unnecessary <p></p>. Does anyone see what could be
going wrong?

I'd rather not shift through the code you posted, but I can offer an
alternative approach that may work better for you (see also perldoc -q
"more than one line"):

#!/usr/bin/perl
use strict; #use warnings;

$/ = ''; # read in more whole paragraph, not just one line
while ( <DATA> ) {chomp;
print "<p>$_</p>\n";
}

__DATA__
This is the super dooper intro.

this is more of a test than an intro, but whatever.


more stuff


########## OUTPUT ###############3
<p>This is the super dooper intro.</p>
<p>this is more of a test than an intro, but whatever. </p>
<p>more stuff</p>
 
G

Gunnar Hjalmarsson

Basically, what I'm trying to do is take some text as input, split
along groups of newlines (so that multiple newlines in a row have the
same effect as a single one), and make each of the elements of the new
array an HTML paragraph. Here is the relevant subroutine:

#!/usr/bin/perl -w
#use strict;

#sub paragrapher {
# my @paragraphs = split /\n+/, $_[0];

Even if David posted a more elegant way to do it, this might make a
difference:

my @paragraphs = split /\s*\n\s*/, $_[0];
 
A

Anno Siegel

Hello,

I'm working on a project, and I'm having a little bit of trouble.
Basically, what I'm trying to do is take some text as input, split
along groups of newlines (so that multiple newlines in a row have the
same effect as a single one), and make each of the elements of the new
array an HTML paragraph. Here is the relevant subroutine:

#!/usr/bin/perl -w
#use strict;

#sub paragrapher {
# my @paragraphs = split /\n+/, $_[0];
# my $new;
# for (@paragraphs) {
# if (length($_)) {

The test for length is unnecessary. split( /\n+/, ...) can never return
an empty element. /\n+/ would have to match in the middle of a series
of line feeds, which it can't.
# $new = $new . "<p>$_<\/p>";

Better written as

$new .= said:
# }
# }
# return $new;
#}

If we feed the subroutine the text:

This is the super dooper intro.

OMG, this is more of a test than an intro, but whatever.

stored in a variable, the output is

Where does the string begin and end? Don't specify string data in prose,
it's ambiguous. Use Perl code. I'm assuming

my $str = <<EOF;
this is the super dooper intro.

OMG, this is more of a test than an intro, but whatever.
EOF
<p>This is the super dooper intro.</p><p>
</p><p>
OMG, this is more of a test than an intro, but whatever.</p>

Note the totally unnecessary <p></p>. Does anyone see what could be
going wrong?

What are you talking about? For me the output is (all on one line)

<p>This is the super dooper intro.</p><p>OMG, this is more of a test
than an intro, but whatever.</p>

If you add a line feed to each paragraph for readability, as in

sub paragrapher {
join '', map "<p>$_</p>\n", split( /\n+/, shift);
}

the output becomes

<p>This is the super dooper intro.</p>
<p>OMG, this is more of a test than an intro, but whatever.</p>

How is that not what you want?

Anno
 
T

Tad McClellan

Basically, what I'm trying to do is take some text as input, split
along groups of newlines (so that multiple newlines in a row have the
same effect as a single one), and make each of the elements of the new
array an HTML paragraph. Here is the relevant subroutine:

#!/usr/bin/perl -w
#use strict;

#sub paragrapher {
# my @paragraphs = split /\n+/, $_[0];
# my $new;
# for (@paragraphs) {
# if (length($_)) {
# $new = $new . "<p>$_<\/p>";
# }
# }
# return $new;
#}


There _is no_ subroutine there, only a slew of comments.

You have been here long enough to have seen the Posting
Guidelines by now.

If you post a short and complete program that we can run,
then we can surely help you fix it.

But you didn't, so we can't.

If we feed the subroutine the text:

This is the super dooper intro.

OMG, this is more of a test than an intro, but whatever.

stored in a variable,


Please speak Perl whenever possible.

In your prose, the variable many contain anywhere from 3 to 5
newlines in it, since the start and end of the string are
not clearly delimited.

We need the real data if we are to provide a real answer.

the output is

<p>This is the super dooper intro.</p><p>
</p><p>
OMG, this is more of a test than an intro, but whatever.</p>


Not when I do it in a short and complete program:

-------------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $str =<<ENDSTR;
This is the super dooper intro.

OMG, this is more of a test than an intro, but whatever.
ENDSTR

print paragrapher($str);


sub paragrapher {
my @paragraphs = split /\n+/, $_[0];
my $new;
for (@paragraphs) {
if (length($_)) {
$new = $new . "<p>$_<\/p>";
}
}
return $new;
}
-------------------------------------


Does anyone see what could be
going wrong?


Nope.

Could it be that the code you have shown is not the code that
is producing that output?

Your output could be easily explained if there were 2 more
characters than what you have shown us:

my @paragraphs = split /(\n+)/, $_[0];

Do you have parenthesis in your split pattern?

I sure hope not...
 
A

Anno Siegel

Tad McClellan said:
[lots]

Your output could be easily explained if there were 2 more
characters than what you have shown us:

my @paragraphs = split /(\n+)/, $_[0];

Do you have parenthesis in your split pattern?

Very clever guess. It may not be clairvoyance, but can I call you Sherlock?

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top