Shorten String

I am looking for a perl function or easy way to shorten a long string
if its over X characters.

Say I have:

"Doe, John; 56943 Walnut Drive; SomeCity, NY"

It messes up the display of my html form. So I would like it changed to:

"Doe, John; ..... meCity, NY"

Keep first 10 and last 10 characters and abbreviate all else.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
matt [ Di, 05 April 2011 23:25 ] [ ID #2057646 ]

Re: Shorten String

On Tue, Apr 05, 2011 at 04:25:12PM -0500, Matt wrote:
> I am looking for a perl function or easy way to shorten a long string
> if its over X characters.
>
> Say I have:
>
> "Doe, John; 56943 Walnut Drive; SomeCity, NY"
>
> It messes up the display of my html form. So I would like it changed to:
>
> "Doe, John; ..... meCity, NY"
>
> Keep first 10 and last 10 characters and abbreviate all else.

The function you are looking for is substr.

$ perldoc -f substr

You probably want something like:

substr $x, 10, -10, " ... " if length $x > 25;

--
Paul Johnson - paul [at] pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Paul Johnson [ Di, 05 April 2011 23:56 ] [ ID #2057647 ]

Re: Shorten String

On 05/04/2011 22:56, Paul Johnson wrote:
> On Tue, Apr 05, 2011 at 04:25:12PM -0500, Matt wrote:
>>
>> I am looking for a perl function or easy way to shorten a long string
>> if its over X characters.
>>
>> Say I have:
>>
>> "Doe, John; 56943 Walnut Drive; SomeCity, NY"
>>
>> It messes up the display of my html form. So I would like it changed to:
>>
>> "Doe, John; ..... meCity, NY"
>>
>> Keep first 10 and last 10 characters and abbreviate all else.
>
> The function you are looking for is substr.
>
> $ perldoc -f substr
>
> You probably want something like:
>
> substr $x, 10, -10, " ... " if length $x> 25;

Very neat Paul. My hat's off to you.

Rob


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Mi, 06 April 2011 03:47 ] [ ID #2057648 ]

Re: Shorten String

>> You probably want something like:
>>
>> =A0 substr $x, 10, -10, " ... " if length $x> =A025;
>
> Very neat Paul. My hat's off to you.

Worked perfectly too. Thanks Paul.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
matt [ Mi, 06 April 2011 17:50 ] [ ID #2057660 ]

Re: Shorten String

>>>>> "M" =3D=3D Matt <lm7812 [at] gmail.com> writes:

>>> You probably want something like:
>>>
>>> =A0 substr $x, 10, -10, " ... " if length $x> =A025;
>>
>> Very neat Paul. My hat's off to you.

M> Worked perfectly too. Thanks Paul.

the real question is whether you read the docs on substr and understand
how that code works. getting code for a problem isn't going to help you
learn perl better unless you read the docs and understand what substr
does and its options. substr is a core function that all experienced
perl hackers know and you should too. did you read its documentation? do
you understand it?

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Mi, 06 April 2011 18:14 ] [ ID #2057661 ]
Perl » gmane.comp.lang.perl.beginners » Shorten String

Vorheriges Thema: Oracle nightmare
Nächstes Thema: Capturing the output of a shell command