Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: japhy blabs about regexes (again)

by Wookie (Beadle)
on Jul 17, 2001 at 16:47 UTC ( [id://97297]=note: print w/replies, xml ) Need Help??


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:??;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://97297]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2025-03-18 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (56 votes). Check out past polls.