in reply to Re: Parsing a Simple Log File
in thread Parsing a Simple Log File
Building on that base, I suggest...
Unless you are absolutely sure that the input will always be in exactly the right form (now and in the future), it's a good idea to do something when the regex doesn't match -- at least a diagnostic message indicating that something isn't as expected. Along the lines of:
Of course you could make the warning message more helpful, or do something else to flag the problem.if (my ($title, $row) = /TEST\s+\w+\W+(\w+)\D+(\d+)/) { push @records, [$title, $row]; } else { warn "no match at $." ; } ;
In passing, when I checked this fragment I noted that the regex is "widely drafted" (as the lawyers would say). For real (as opposed to example) code it's a good idea to tighten up the regex, so that it doesn't happily provide duff results from duff data.
|
|---|