$_ = "1 2 3 4 5 6 7 8 9 0";
/(.*)(\s.*){4}/;
print "$1\n";


-----Original Message-----
From:   Aaron J Mackey [mailto:[EMAIL PROTECTED]] 
Sent:   Monday, October 14, 2002 7:19 PM
To:     [EMAIL PROTECTED]
Subject:        correctly rebuilding a whitespace-split string


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.  One method:

$_ = <DATA>;
@d = splice(split, -4); # I always know that the last 4 fields
                        # should be removed ...

# this next line is the one to focus on:
$str = join("", (split(/(\s+)/, $_, scalar(@d) + 1))[0..2*$#d]);

ok($str, "This is  the string I want");

__DATA__
This is  the string I want  AND THIS IS BOLLOCKS

__END__

method 2:

($str) = m/^(\S+(?:\s+\S+){$#d})/;

Thanks,

-Aaron

Reply via email to