From: David Vallner Date: 2006-10-20T09:51:47+09:00 Subject: Re: challenge: correctly handling Accept-Language HTTP header --------------enig13035FB3AA5E2804F508CD71 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable khaines@enigo.com wrote: > If one has a list of en, en-gb and es, and one gets an Accept-Language > header like this: >=20 > es-mx,es;q=3D0.8,en-us;q=3D0.5,en;q=3D0.3 >=20 > the code would return the 'es' as the best matching language option. >=20 I have some doubts you'll ever see that sort of header in the wild. Besides, parsing that one is hardly something that needs a C library. Here's an almost-one-liner, if you're willing to forgive the each block being three statements and remove all whitespace (it actually parses...) "es-mx,es;q=3D0.8,en-us;q=3D0.5,en;q=3D0.3".split(",").map { | i | i.split(";") }.each { | i | i[1] =3D~ /q=3D(\d+\.\d+)/; i[1] =3D $1 && $1.to_f || 1.0 }.sort_by{ | i | i[1] }.reverse[0][0] =3D> "es-mx" (The quality value for es-mx defaults to 1, es is only 0.8) David Vallner (That's by far the ugliest code I've written in a year.) --------------enig13035FB3AA5E2804F508CD71 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) iD8DBQFFOB2jy6MhrS8astoRAtLvAJ9Y2622jFm67RtzAwAxNZNhjsc1WwCcC9vx g7u0k6RYdoZ8zeIktQEBBKo= =DJeg -----END PGP SIGNATURE----- --------------enig13035FB3AA5E2804F508CD71--