Hi Mimi,

> 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?

This might work for you I guess:

<Code>
#!/usr/bin/perl
$s = "The black cat climbed the green tree";
$count = 50;
$s =~ /(^[\S\s]{1,$count}\S*)(\s.*|$)/;
print "$1\n";
</Code>

when $count is 30, it prints:
The black cat climbed the green

when $count is 50, it prints:
The black cat climbed the green tree

My knowledge in Perl is limited, so there may be a more apt solution in your 
case.

Regards,
Akhthar Parvez K
http://Tips.SysAdminGUIDE.COM
UNIX is basically a simple operating system, but you have to be a genius to 
understand the simplicity - Dennie Richie
On Sunday 18 Apr 2010, 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".
> 
> 
>  
> 
>  
> 
>  
> 
> 



-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to