powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

I have a database with a TEXT field, which could have just one paragraph, or as many as 50 paragraphs. More likely it will have more than 10. I also have another database table with a number of sponsors that could be 0-10. No more than 10 ever.

I need to find a way to have it where the text that is displayed will automatically insert one sponsor for every equal number of paragraphs.

Such as this. Say there are 3 sponsors and 10 paragraphs, I would like it to print 3 paragraphs, then insert a table with the first sponsor, then print another 3 paragraphs, then insert another table with the second sponsor, then print another 3 paragraphs, then the final sponsor table, and then the last paragraph.

It really does not have to be in between the paragraphs, I guess it could be between words, but how can I just find equal places to put the sponsors in the text?

Would it work to count the "length" of the text, then divide that by the number of sponsors, then find the next "space" after the first number?(Such as this: The text field has a length of 3000 characters, and there are 3 sponsors, then there would be 1 sponsor after every 1000 characters.)

How would I find the next space between words, AFTER 1000 characters(in the above example)?

Or do you know of a better way to do this?

I would appreciate any tips on a way to do this.

Thank you!
Richard

Edited by BazB - fixed .sig div tags.

Replies are listed 'Best First'.
Re: Parsing and adding to a "variable".
by Vautrin (Hermit) on Jan 24, 2004 at 04:39 UTC

    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!

      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);.

Re: Parsing and adding to a "variable".
by QM (Parson) on Jan 24, 2004 at 03:57 UTC
    How do you know when a paragraph ends?

    -QM

    --
    Quantum Mechanics: The dreams stuff is made of
      good question.... Hmm, I guess I'll have it replace two return's with a </p><p>

      Yeah, that is what I'll do since it won't contain any html formatting, more than likely.

      Thanks!
      Richard