in reply to japhy blabs about regexes (again)

Surely all this depends on how much white space you're typically expecting. Could the following not also be more efficent depending on the data:
if ($blah=~m/^\s/) { $blah=~s/^\s+//; }
or indeed (thanks to MZSanford for this one):
if (index($blah," ")) == 0 { $blah=~s/^\s+//; }
and for trailing - use rindex :).


UPDATE: Oops - The index solution only works for *actual* spaces (thanks Hofmator).

That possibly gets you a little closer to the best of both worlds?
game(Wookie,opponent) eq 'Wookie' ? undef $problem : remove_limbs(arms,opponent);

Replies are listed 'Best First'.
Re: Re: japhy blabs about regexes (again)
by japhy (Canon) on Jul 17, 2001 at 18:45 UTC
    The problem isn't leading spaces. Leading spaces are terribly easy: ^\s+ says "find the beginning of the string and one or more spaces". It's trailing spaces that are hideous. Instead of \s+$ meaning "find the end of the string, and one or more spaces before it", it means "find one or more spaces, and then try for the end of the string". That's sad of it.

    _____________________________________________________
    Jeff japhy Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;