Solved

perl, shorten the text string

Posted on 2010-11-18
3
613 Views
Last Modified: 2012-05-10
my $longString = "Los Angeles, California, United States of America";

If  a string is more than 20 character, I want to replace the rest with "...", so that $longString will be "Los Angeles, Califor ...";
0
Comment
Question by:rgbcof
3 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 125 total points
ID: 34166173
$string = substr($string,0,20).'...' if length($string) > 20;
0
 
LVL 12

Expert Comment

by:tel2
ID: 34167716
Hi rgbcof,

You could do this:

    $longString =~ s/^(.{20}).+/$1.../;

or if you want a space before the dots, as in your example, do this:

    $longString =~ s/^(.{20}).+/$1 .../;
0
 

Author Closing Comment

by:rgbcof
ID: 34168725
Works well.  Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl to display a matrix report for 3-dimenional array 3 63
PHP equivalent of opening a com object 5 69
Migrate OTRS to newest version. 1 309
iSeries PERL Scripts 7 146
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now