Re: truncating a line
by ctilmes (Vicar) on Jul 01, 2003 at 13:56 UTC
|
use the substr() as an lvalue:
substr($str, 17) = '...' if length $str > 17;
| [reply] [d/l] |
Re: truncating a line
by tilly (Archbishop) on Jul 01, 2003 at 13:52 UTC
|
Use length to find the length, and then substr to chop it.
BTW a tip. It works slightly more smoothly if you only chop and add the dots if the string is over 20 characters. | [reply] |
|
|
BTW a tip. It works slightly more smoothly if you only chop and add the dots if the string is over 20 characters.
What exactly do you mean by this?
is there a technical reason,
or because it cuts a word in half with that particular string,
or in your experience 20 chars is best to still leave the string reasonably understandable?
Cheers,
fireartist
| [reply] |
|
|
| [reply] |
|
|
Re: truncating a line
by dreadpiratepeter (Priest) on Jul 01, 2003 at 13:51 UTC
|
$str = length($str)>17 ? substr($str,0,17).'...' : $str;
-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere." | [reply] [d/l] |
Re: truncating a line
by particle (Vicar) on Jul 01, 2003 at 13:55 UTC
|
my $long_line= 'this line is more than seventeen characters.';
my $short_line= substr( $long_line, 16, length $long_line ) .= '...';
~Particle *accelerates* | [reply] [d/l] |
Re: truncating a line
by BrowserUk (Patriarch) on Jul 01, 2003 at 14:16 UTC
|
$line = $1 . '...' if $line =~ m[^(.{17})];
...but ctilmes method above is the right way:)
Update: Corrected (another) typo pointed out by sauoq .
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
| [reply] [d/l] |
Re: truncating a line
by hardburn (Abbot) on Jul 01, 2003 at 14:17 UTC
|
Just to be different:
# $str defined elsewhere
$str =~ s/\A
(.{0,17})
(.*) # Normal rules about .* don't apply--we
# really do need everything that's left
\z/$1(?($2) \. \. \.)/x;
Update: Never mind. Doesn't work.
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] [d/l] |
Re: truncating a line
by svsingh (Priest) on Jul 01, 2003 at 15:26 UTC
|
I haven't seen anyone answer with this one yet, so I thought I'd throw it out there. Sorry if it's a repeat. I'm not as proficient with all the regex tricks as I'd like to be.
$line =~ /^(.{0,17})/;
print "$1\n";
If that's a rehash, then please let me know. I think it's close to BrowserUK's answer, but they have an if that mine doesn't. Thanks. | [reply] [d/l] |
|
|
| [reply] |
|
|
Argh. Thanks tilly. I was so jazzed that I knew how to approach the first half, I overlooked the second. Read the spec, read the spec, read the spec.
| [reply] |