Best way to remove last word from a string

G

Guy Smiley

Hello,

After googling and perldoc'ing, I'm still struggling to figure out the best
way to remove the last word from a string (without any line-ends or carriage
returns).

$output = "data1 data2 data3 data4";

Would like to see:

data1 data2 data3

I need to get this done soon and I have a long way to go before I understand
how to do this with a regex.

I'm also thinking that I could split the string at the spaces and construct
a new string by iterating through the array count minus 1. But this seems
like overkill.

my @datas = split(' ', $output);
for-loop
etc;

Any insight is greatly appreciated.
 
G

Gunnar Hjalmarsson

Guy said:
After googling and perldoc'ing,

Which parts of the docs have you studied?
I'm still struggling to figure out the best way to remove the last
word from a string (without any line-ends or carriage returns).

$output = "data1 data2 data3 data4";

Would like to see:

data1 data2 data3

I need to get this done soon and I have a long way to go before I
understand how to do this with a regex.

Then you'd better start right away, eh?

perldoc perlrequick
I'm also thinking that I could split the string at the spaces and
construct a new string by iterating through the array count minus
1. But this seems like overkill.

my @datas = split(' ', $output);
for-loop
etc;

Well, if you start with splitting like that, you can make use of the
pop() function instead of iterating through the array. Doing so would
not be overkill IMO.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top