I thought there was a tie module for this but a quick scan of the first couple of hundred didn't find it. So here's one.
As Limbic~Region point's out, I was looking in the wrong namespace. As well as Hash::Case::Preserve, there is also Hash::Case::Lower. The difference between the two (besides the obvious) is that the former uses two hashes internally. One for allowing the case-insensitive matching, the other for preserving the original case of the keys for when iterating with keys or each.
The also showed me that I forgot a couple of bits below. (Now added.)
#! perl -slw use strict; package Tie::Hashi; use Tie::Hash; our @ISA = 'Tie::ExtraHash'; sub TIEHASH{ bless [{}], $_[ 0 ]; } sub STORE { $_[ 0 ][ 0 ]{ lc $_[ 1 ] } = $_[ 2 ]; } sub FETCH { $_[ 0 ][ 0 ]{ lc $_[ 1 ] }; } sub EXISTS{ exists $_[ 0 ][ 0 ]{ lc $_[ 1 ] }; } sub DELETE{ delete $_[ 0 ][ 0 ]{ lc $_[ 1 ] }; } 1; package main; tie my %hashi, 'Tie::Hashi'; $hashi{ TheKey } = 12345; print $hashi{ tHeKeY }; print keys %hashi, $/, values %hashi; __END__ P:\test>tiehashi.pl 12345 thekey 12345
In reply to Re: case-insensitive hash keys
by BrowserUk
in thread case-insensitive hash keys
by ManFromNeptune
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |