in reply to How can you split inside pop?

Other monks have corrected your syntaxing.

However, if you only want the last word in the string, why don't you use a regular expression to fetch it, without having to split the string first?

my $str = "this is a string"; print $str =~ m/\w+$/g;
I believe this is the most efficient method of finding the last word in the string.

Replies are listed 'Best First'.
Re: Re: How can you split inside pop?
by Anonymous Monk on Oct 15, 2003 at 05:22 UTC

    \w isn't the same as \S. Try "string's" for a very good reason why this doesn't do what you expect. Also, suppose you have the string "this string has a space at the end ". Your snippet doesn't work properly yet split does since any null fields at the end are stripped. To get the same behavior, you'd need something like the following:

    print $str =~ /(\S+)\s*$/;

    As for speed, it is faster.