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. Advertisements

  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. Advertisements

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:
    1,139
    Steven D'Aprano
    Oct 23, 2005
  2. shapper

    Remove first word of a string

    shapper, Oct 1, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    942
    Siva M
    Oct 1, 2006
  3. Johny
    Replies:
    8
    Views:
    689
  4. shapper

    Remove last letter from string

    shapper, Oct 22, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    630
    shapper
    Oct 22, 2007
  5. Kevin
    Replies:
    16
    Views:
    48,574
    Roedy Green
    Jan 30, 2008
  6. shapper

    Remove word from string

    shapper, Feb 14, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    543
    Mark Rae [MVP]
    Feb 14, 2008
  7. PeroMHC

    remove last 76 letters from string

    PeroMHC, Aug 6, 2009, in forum: Python
    Replies:
    6
    Views:
    394
    Iain King
    Aug 6, 2009
  8. Grant Edwards

    Idiom for "last word in a string"

    Grant Edwards, Sep 23, 2009, in forum: Python
    Replies:
    6
    Views:
    383
    Peter Otten
    Sep 23, 2009
Loading...