On 7/26/06, Rob Dixon <[EMAIL PROTECTED]> wrote:
[sarcasm snipped]
> If you want numbers, look for numbers. Because if there's a typo in the file, > Perl will quite happily go on and use non-numeric matches of \S in subsequent > arithmatic, yeilding unexpected and difficult to debug results, e.g. > > $a = "F"; > print $a + 0; What your attribution above doesn't show is that this is a general purpose subroutine which takes a filename as its parameter and returns a hash value describing all lines in that file looking roughly like param = value The intention was that, once the data was in a Perl data structure, the calling code could make sure that the required values were both present and valid. In the case of your given data subroutine would return a list that, when assigned to a hash, would look like: my %params = ('$a' => '"F"'); and the second line would be ignored as it contains no '=' character. It was never my intention to parse Perl source. Usage would be something like
I think you missed my point. That wasn't meant to be some example of a badly formed .ini file. It was meant toillustrate the point for people who haven't thought about it before. It doesn't have anything to do with file structure. Don't put that code in a file. Run it. or try this: perl -e '$a="F"; print $a + 0" If it's not something you've tried before, the result might surprise you. [snip]
> Using $1 and $2 in this context is a bit redundant, too. instead, try > something like > > my ($condition, $value) = /\s*(\w+)\s*=\s*(\d+)/; This is nonsense. You choose to declare and assign two additional scalar variables with the same contents as the already-existing $1 and $2, just to push them onto an array and discard them again? It is your alternative that is redundant. And look at the ugly code you get if you try to both validate and parse the record in the same statement as did my 'next unless //'. It would be nice to see what a full alternative solution would look like using your ideas.
Apologies. I misread your code. Feel free to modify the code to suit whatever structure you want to put it into, but hold onto this simple advice: If you want to get numbers out of string, use a regex that looks for numbers, not one that looks for "not white space". Implementation details are, as always, left as an exercise for the reader. /\s*(\w+)\s*=\s*(\d+)/ gives you, in the words of the slogan, "everything you want, and nothing you don't." -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!