Bart Lateur wrote:
> 
> On Mon, 14 Oct 2002 20:19:10 -0400 (EDT), Aaron J Mackey wrote:
> 
> >Someone have a more "perlish", elegant, or just plain faster way of doing
> >something like this: split a string on white space, pop off the last 4
> >fields (perhaps to be used elsewhere), and then "rebuild" the original
> >string with the correct amount of intervening whitespace.
> 
> You mean with the same amount of whitespace as before? None of the
> solutions I've seen does that.
> 
> Perhaps this is one way:
> 
>         $_ = 'This is  the string I want  AND THIS IS BOLLOCKS';
>         {
>             my @end;
>             push @end, $+[0] while/\S+/g;
>             splice @end, -4;
>             substr($_, pop @end) = "";
>         }
>         print;
> 
> No, it's not really *that* elegant. But it does put two spaces between
> "is" and "the", just as in the original.

This is surely dicey if there are two elements the same?

Why not just:

s/(\s+\S+){4}\s*$//;

$1 for the chopped stuff.

Jasper
-- 
(Hunting a moth)
Hank Spim: Well, I follow the moth in the helicopter to lure it away 
           from the flowers, and then Roy comes along in the Lockheed 
           Starfighter and attacks it with air-to-air missiles.

Reply via email to