Re: how to shorten given string?
by Yendor (Pilgrim) on Oct 13, 2004 at 18:44 UTC
|
$str = "Ipod batteries for Apple Ipod PDAs. Replacement batteries from
+ Laptops for Less.com.url";
$short = substr($str, 0, 20);
Also, see the substr doc from Perldoc.com.
| [reply] [d/l] |
|
|
What if i want to do it with regexp, is there a way to do it like that?
| [reply] |
|
|
/^(.{1, 20}).*/; # doesn't work
/^(.{1,20}).*/; # does work
/^(.{1,20})/; # even easier
May the Force be with you
| [reply] [d/l] |
|
|
my $short = ( m/^(.{1,20})/ ) ? $1 : '';
...or...
my( $short ) = m/^(.{0,20})/;
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
What if i want to do it with regexp, is there a way to do it like that?
What if I was given a perfectly good working solution, and then I said "No, I want to do it some other way"? Would people think I was asking a homework question? Maybe. Maybe not.
In any event, if this wasn't a homework question, you should have given more background about why substr didn't suffice, so that we know why you rejected that solution.
And it looks like I'm not the only one who wonders this.
| [reply] |
|
|
| [reply] |
|
|
Re: how to shorten given string?
by davido (Cardinal) on Oct 13, 2004 at 18:47 UTC
|
The answer to that question depends entirely on what you consider to be the significant portion of the original string; the portion that you can't afford to lose.
Your example script attempts to just truncate the source string at 20 characters. You can do that quickly like this:
my $short_string = substr $_, 0, 20;
But that truncates your string to become: "Ipod batteries for A". Are you ok with that? Or do you need to define a more complex set of rules as to how the original string should be shortened?
| [reply] [d/l] |
Re: how to shorten given string?
by fglock (Vicar) on Oct 13, 2004 at 20:16 UTC
|
By the way, there is a package that does this:
use String::Escape;
print String::Escape::elide( "Ipod batteries for Apple Ipod PDAs. Repl
+acement batteries from Laptops for Less.com.url", 20 );
# Ipod batteries...
| [reply] [d/l] |
Re: how to shorten given string?
by borisz (Canon) on Oct 13, 2004 at 18:49 UTC
|
$_ = "Ipod batteries for Apple Ipod PDAs. Replacement batteries from L
+aptops for Less.com.url";
print substr ($_, 0, 20);
/^(.{0,20})/;
print $1;
| [reply] [d/l] |
Re: how to shorten given string?
by ysth (Canon) on Oct 13, 2004 at 18:55 UTC
|
| [reply] [d/l] [select] |
Re: how to shorten given string?
by TedPride (Priest) on Oct 13, 2004 at 19:29 UTC
|
my $str = '1234 6789 1234 6789 1234 6789 ';
my $printstr = substr($str, 0, 20+1);
$printstr =~ s/\W+\w*$//;
print $printstr;
Unless I'm missing something important, this should cut the line at the right spot. Note that it's 20+1 because the length has to be max length + 1 so it won't cut a word that fits entirely into the string.
EDIT: You may have to change the regex if words can include characters other than \w, such as . | [reply] [d/l] |