Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by davido (Archbishop)
on Jul 19, 2004 at 15:34 UTC ( #375586=note: print w/replies, xml ) Need Help??


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

Maybe I'm missing something?

Only that I was just providing a starting point to illustrate a method. I figured you would tweak it to meet your needs. But in fairness, I should have provided an answer that produced exactly what you were looking for. I've modified my answer to do what I think your question was asking, and will paste it below...

The problem with my original (I think) was that it broke your original string into substrings of 'n' characters. What you were looking for was simply truncation, with trailing '...' elipses. Here ya go...

use strict; use warnings; my $len = 40; print "0123456789012345678901234567890123456789\n"; while ( my $string = <DATA> ) { chomp $string; $string =~ s/((?:.{0,$len}(?=\s|$))|(?:\S{$len}))(?:.+$)?/$1.../s; print "$string\n"; } __DATA__ Now is the time for all good men to come to the aid of their country. Nowisthetimeforallgoodmentocometotheaidoftheircountry.

Hope this helps.


Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://375586]
help
Chatterbox?
[marioroy]: Back in HighSchool, I came late to Gym class. There was a fellow who was picked last, no team wanted him for racketball. So it was us two against everybody. I looked at him in the eye and said to let your body loose and let the powers to be help us win.
[marioroy]: We made it all the way to the end. The expience was magical. I not good at sports. Someone or thing was there.
erix considers extracting a catalog ...
[karlgoethebier]: marioroy: Racket ball is considered harmful, like Squash
[marioroy]: Wait till folks see what MCE::Hobo can do in the upcoming release. Multiple instances, nested spawning, 3 levels, doesn't matter. it works on Windiows too.
[Lady_Aleena]: I hate sites that tell me to turn ad-blocker off. They should know better.
[marioroy]: s/Windiows/ Windows/
[Discipulus]: marioroy can you expand that Niagara's fall history?
erix only sees fallen angels
erix if it weren't for bad luck, I'd have no luck at all

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2017-05-29 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?