in reply to On creating hash whose keys aren't case-sensitive
And just for the fun of it: your _lc rewritten into one regex:
This sub returns undef (indicating an error) if you give it a string with more than one internal dash or underscore.use Modern::Perl; sub _lc { my $key = shift; my $success = $key =~ s/^-?([^-_]*)[-_]?([^-_]*)$/$2?lc"$1_$2":lc$ +1/e; return $key if $success; } print _lc($_) while (<DATA>); __DATA__ content-length content_length -content-length -content_length Content-Length Content_Length -Content-Length -Content_Length changed Changed -changed -Changed Too-many_dashes-Or-underscores
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
My blog: Imperial Deltronics
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: On creating hash whose keys aren't case-sensitive
by tobyink (Canon) on Feb 18, 2012 at 13:12 UTC | |
by CountZero (Bishop) on Feb 18, 2012 at 22:00 UTC | |
|
Re^2: On creating hash whose keys aren't case-sensitive
by anazawa (Scribe) on Feb 18, 2012 at 15:20 UTC |