Best way to remove last word from a string

Discussion in 'Perl Misc' started by Guy Smiley, Aug 3, 2004.

  1. Guy Smiley

    Guy Smiley Guest

    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.
     
    Guy Smiley, Aug 3, 2004
    #1
    1. Advertising

  2. Guy Smiley wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 4, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    20
    Views:
    912
    Steven D'Aprano
    Oct 23, 2005
  2. Johny
    Replies:
    8
    Views:
    394
  3. shapper

    Remove last letter from string

    shapper, Oct 22, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    501
    shapper
    Oct 22, 2007
  4. Kevin
    Replies:
    16
    Views:
    47,307
    Roedy Green
    Jan 30, 2008
  5. PeroMHC

    remove last 76 letters from string

    PeroMHC, Aug 6, 2009, in forum: Python
    Replies:
    6
    Views:
    321
    Iain King
    Aug 6, 2009
Loading...

Share This Page