Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: DWIM on sub inputby Excalibor (Pilgrim) |
on Jul 16, 2003 at 16:01 UTC ( [id://274890]=note: print w/replies, xml ) | Need Help?? |
Hi there, Why not something in the line of this? :
I think this is easy to follow. We first match for three non negative real numbers (allowing numbers such as .01). If it's between 0 and 255 why suppose -5 is zero instead of a typoed '-' sign? The client should know the contract better and write the string properly. Anyway, it admits strings of the form "255, 125,and 7.34" for example (so much for flexibility). We put the results into a properly named hash (very useful if we need to make more calculations with the colors, not just this simple thing). We normalize the values in-place and return what we want, a ref to an array (we can return a list wich will work as well, but a ref seems tidier). Granted, it's more verbose, but it's also much cleaner, I think. Want a golfer solution? Easy:
Best regards, Update: oops, forgot to normalize to 1, corrected, now 400.23 => 144. They definitely should read the sub contracts... :-) --
In Section
Seekers of Perl Wisdom
|
|