Mimi Cafe wrote:
I used MySQL substr function to extra 100 characters from the result of a
query, but understandably, I don't get what I want.

Now I looked at Perl's substr function and it doesn't look like it can help
me achieve what I need to.

Let's say I have:

$s = "The black cat climbed the green tree";

$substring = substr( $s, 1, 15); # this will return "The black cat c".

No it will not. It will return "he black cat cl" because in perl offsets start at 0 and not 1:

$ perl -le'
my $s = "The black cat climbed the green tree";
my $substring = substr( $s, 1, 15 );
print $substring;
'
he black cat cl


How can I have this return the whole word climbed rather than the c (i.e. I
need to get "The black cat climbed")? I need to get the remaining characters
from the length till the next white space or end of a phrase.

Any other way to overcome this limitation?  How can I use regex here?

$ perl -le'
my $s = "The black cat climbed the green tree";
my $length = length $s;
my ( $substring ) = $s =~ / \A ( .{15,$length}? \b ) /x;
print $substring;
'
The black cat climbed




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to