Re: Fell off the Llama
by Roy Johnson (Monsignor) on Jun 24, 2004 at 18:40 UTC
|
It can be hard to focus on things when you've been through a lot of emotional events, so you might just need a little more time before you climb back on. But if you feel like you're really ready, here's as good a job as I can do to make the exercise even more clear.
The exercise is dealing with regular expressions (which the book is calling patterns), which are a kind of string-description language.
The Perl operator for applying a regular expression to a string to see if it matches, is the "match operator", m//. The m is optional (and generally omitted) if slashes are used. The regular expression goes between the slashes. To see if $_ matches $pattern, we just do /$pattern/.
To see if the pattern in $what appears somewhere in $_ three times in a row, we can do /$what$what$what/, or /($what){3}/ for short.
We're not really tightening our belts, it just feels that way because we're getting fatter.
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
Re: Fell off the Llama
by theorbtwo (Prior) on Jun 24, 2004 at 18:01 UTC
|
| [reply] [d/l] |
|
|
/($what){3}/
The OP quoted it wrong.
| [reply] [d/l] |
|
|
ORA's usually pretty good about maintaining errata for their titles. I don't see this particular typo listed, so the original poster might want to use the submit link on that page and let them know (presuming it's the 3rd edition we're talking about; I didn't check the errata for the prior editions).
| [reply] |
Re: Fell off the Llama
by pbeckingham (Parson) on Jun 24, 2004 at 19:06 UTC
|
The stated answer includes a space between the right paren and the brace, which means that it won't work unless the x is added, like:
/(what) {3}/x
Deleting the space will also work:
/(what){3}/
But it does capture, and it doesn't have to if that is not what's needed - just use non-capturing grouping:
/(?:what){3}/
| [reply] [d/l] [select] |
|
|
Your answer doesn't quite match the question: "Make a pattern that will match three consecutive copies of whatever is currently contained in $what."
Your solution fails to match what is contained in $what; it mearly matches the string 'what'.
Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).
| [reply] |
Re: Fell off the Llama
by ysth (Canon) on Jun 25, 2004 at 05:09 UTC
|
I am now a "monk". While I appreciate the honor, I have to say that I am truly not worthy of that title. I am a seeker, not a dispenser of perl wisdom.
Seekers are also worthy of advancement through the ranks. See Sainthood via Seniority Simulation.
| [reply] |
Re: Fell off the Llama
by talexb (Chancellor) on Jun 25, 2004 at 16:33 UTC
|
My sympathies on your recent bout of bad fortune. Perhaps now is a bad time to try and wrap your head around OO concepts -- I wouldn't work too hard on that right now, rather, I'd probably take some time off. Put the brain in neutral, relax, go for long walks, sleep in, let the body and the mind heal.
You'll know how long that period needs to be -- then you can dive into the Llama again. Good luck!
Alex / talexb / Toronto
Life is short: get busy!
| [reply] |