I am using Perl to generate a SELECT pull-down in an HTML form. I'd like to limit the length of the text that gets shown, so that the menu doesn't become too wide. I'm trying to figure out the best method for doing this.

For the sake of argument, lets say...

  • I want a maximum string length of 15 characters
  • The text for the OPTION is coming from a database, and can contain spaces and punctuation
  • I want to chop long entries between words and add a "..." to the end

    Thought 1

    1. Compare string length against maxlength
    2. If stringlength>maxlength, use a regex to chop off the end at a whitespace, and concatonate a "..."
    3. Compare new stringlength against maxlength and repeat if necessary

    Thought 2

    1. Similar to above, but use split to put all of the words into a temporary array, then reconstruct the string, leaving out words as necessary

    There are probably other ways as well.

    How would YOU do it?


    In reply to Smart Substrings by cei

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.