in reply to Smart Substrings

Honestly, you'd be better off with $string = substr($string,0,12).'...' if length($string)>15; Since sooner or later you will wind up with a string like "a misunderstanding" and your "smart" substring will leave you with "a..." and really, what good is that? =)

Once you add in a minimum string length, you are just plain going to be doing too much work for the benefit.

--
$you = new YOU;
honk() if $you->love(perl)

Replies are listed 'Best First'.
Re: Re: Smart Substrings
by cLive ;-) (Prior) on Apr 03, 2001 at 11:52 UTC
    you know,

    I *did* think of that, but was happier giving a 3 line solution :)

    I used a similar regex in one of my scripts to split a line as close to 70 chars as possible without breaking in a word). To avoid the problem you suggest, I would use this:

    unless ($string =~ s/^(.{6,12})\b\s.*/$1.../gi)
    But for such a small string it's a little rough. The context I use it in is {50,70} for line breaks, and that seems to work just fine.

    I think the *idea* is sound though, and I'm sure the user could amend .{0,12} to .{4,12} or .{7,12} depending on their specific requirements to get it to work for them.

    cLive ;-)