good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Hurdle with summarizing results from complex data structure related to MySQL result handlingby Athanasius (Archbishop) |
on Feb 17, 2015 at 16:11 UTC ( [id://1117003]=note: print w/replies, xml ) | Need Help?? |
Hello hiyall, Feedback and critique is welcome. roboticus has already provided an excellent answer, so I’ll just comment on one aspect of your code:
First, there is no point in testing the length of each string, because the capture groups are all quantified with a +, meaning “one or more” — so if a match succeeds at all (defined is true), the length must be at least 1. Second, there is no point in separately testing whether each string is defined, as the regex — any regex — either succeeds or it fails, so either all the match strings are defined, or none of them are.1 Third, note that the trailing /x does nothing here, as you don’t have any whitespace in the regex. So the above code can be better written like this:
1Update: Except in the cases where the captures are optional, either because zero matches are allowed: X?, X*, X{0,5}; or because the captures are part of an alternation: (?:(X)|(Y)). Thanks to AnomalousMonk for explaining this, below. Hope that helps,
In Section
Seekers of Perl Wisdom
|
|