Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: In search of a better way to trim string length

by Vennis (Pilgrim)
on Jul 19, 2004 at 12:19 UTC ( #375530=note: print w/ replies, xml ) Need Help??


in reply to Re: In search of a better way to trim string length
in thread In search of a better way to trim string

sub trimTo { my ($line, $length) = @_; $line=~s/([^ \s]{$length})([^ \s])/$1...\n$2/g; return $line; }
Though only the first line is 30, and the rest is 31 chars, i think it's more or less doing what you search for ?
It also makes sure strings that are exact 30 length dont have dots added.

I just saw Jaspers solution, it's a bit different, i thought i post it anyway.

Addition:
Code isn't doing what was expected. Sorry Kiat :-)


Comment on Re^2: In search of a better way to trim string length
Download Code
Re^3: In search of a better way to trim string length
by kiat (Vicar) on Jul 19, 2004 at 13:31 UTC
    Hm...yours doesn't produce the desired outputs.

    Given the following inputs:

    1) "thisisthelongesttopicthatwilleverbepostedinperlmonks"

    2) "this is the longest topic that will ever be posted in perlmonks"

    Desired outputs:

    1a) thisisthelongesttopicthatwille ...

    2a) this is the longest topic ...

    Yours gave the following outputs:

    1a) thisisthelongesttopicthatwille...
    verbepostedinperlmonks

    2a) this is the longest topic that will ever be posted in perlmonks

    Did I miss something?

      Nope, you missed nothing, i missed something :-) Never mind that code then, i'll leave it in case somebody finds it either useful or inspiring.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://375530]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2015-07-07 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls