On Wed, 2 Jul 2003, Ling F. Zhang wrote:

> I use /pattern/g to progressively match a string (who
> content is read from a file)
> I want the cut the string off after the n-th match (if
> there is no n-th match, the string is unaltered)
> how can I do this without using a loop?
> right now, I am doing this:
>
> while ($string =~ /pattern/g){
>   $count++;
>   if ($count > $max_count){
>     $string = substr($string,0,pos($string));
>     last;
>   }
> }

How about:

$string =~ s/((.*?$pattern){$max_count})(.*)/$1/s;


George P.

>
> I know about $', $` (and what's the third one?), but I
> just can't figure out how to apply them in this case.
>
> thank you!
>
> __________________________________
> Do you Yahoo!?
> SBC Yahoo! DSL - Now only $29.95 per month!
> http://sbc.yahoo.com
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to