Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Ah... the original post said 'around 100 characters' not that that was the maximum. But no matter.

Sigh. I suppose I did commit the cardinal sin of posting Sloppy code.

And, I should have been clear that what I posted was NOT a turnkey solution but a suggestion that a regex approach might make sense.

so... OK, below is the result of another few minutes fiddling, this will work better, certainly.

my $string; $string = 'lasdufaner%.alsdfi,' x 100; # $string = 'freddy\'s wife wilma, ' x 100; my $max = 100; if ( $string and length $string > $max ){ $string = substr( $string, 0, $max); my ($tmp) = $string =~ /(.+)\s.*?$/; # last space if possible $tmp or ($tmp) = $string =~ /(.+)\W.*?$/; # bust on last non-word $tmp and $string = $tmp; print $string }


freddy's wife output:
freddy's wife wilma, freddy's wife wilma, freddy's wife wilma, freddy's wife wilma, freddy's wife

lasd... output
lasdufaner%.alsdfi,lasdufaner%.alsdfi,lasdufaner%.alsdfi,lasdufaner%.alsdfi,lasdufaner%.alsdfi

The point being, I suppose, that this sort of thing might be easily handled by a regular expression in most cases.

Thanks for your comment though, it's always good to have a second set of eyes. :-)

\s

In reply to Re^3: substr question by stevenmay
in thread substr question by Anonymous Monk

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (5)
    As of 2018-06-21 01:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?



      Results (117 votes). Check out past polls.

      Notices?