in reply to Parsing and adding to a "variable".

What database are you using? Be careful! Some databases, like MySQL, limit the TEXT size (in the case of mysql if you try to send text to the database larger then the packet size compiled in you will have your TEXT truncated).

Whatever you decide to deliniate paragraphs with, (some suggestions: m/\n\n/, m/\n\t/, m/\r\n/, whatever else you decide, use a regular expression and the split command, i.e. my @paragraphs = split /\n\n/, $the_text. Then you can use my $number = scalar (@paragraphs) (scalar not necessary because it's in scalar context but used for clarity). Remember to use the %, modulus operator, to get the remainder when dividing so you don't get a weird decimal!

Replies are listed 'Best First'.
Re: Parsing and adding to a "variable".
by powerhouse (Friar) on Jan 24, 2004 at 06:26 UTC
    I am using MySQL. When I create a newsletter using the system, when I hit submit, I have it first get the 'text', then parse it, adding it to paragraphs:
    my $_content_to_add = param("content"); $_content_to_add = join '', map { "<p>$_</p>\n" } split /(?:\r?\n)+/, +$_content_to_add;
    So now in the database, the paragraphs are in the correct tags.

    I guess I could count them, but how can I count </p>\n<p> Entries in the text?

    Thanks,
    Richard

      Just expand out that one line expression so that you're saving the value of the number of paragraphs (i.e. by putting it into an array before putting it into $_content_to_add). Or I suppose you could just try: my $number_of_paragraphs = scalar (split /(?:\r?\n)+/, $_content_to_add);.

        Most encouraging! Thank you Vautrin.... my $number_of_paragraphs = scalar (split /(?:\r?\n)+/, $_content_to_add); worked! It counted the number of pargraphs :o)

        Thanks again!!
        Richard
        Whoa there, don't do that. split in scalar context will indeed return the number you want, but will have the side effect of replacing the contents of your @_ array. This unfortunate behaviour has been deprecated and will give a warning.