I came upon the need to ensure that the keys for values slurped into a nested hash via XML::Simple reading an XML config file matched what I was referring to them as in my script. After playing around with various Tie::* modules I decided it was just easier for me to force the entire nested hash into one or the other case for my access. The following was the outcome of my trials: Note that duplicate name keys with differing case will be squashed. Now this hash:
$nestedhash = { 'REJECTED' => 'REJECTED', 'FREQUENCY' => 'FREQUENCY', 'INCOMING' => { 'DIR' => 'DIR' }, 'CONNECTIONSTRING' => 'CONNECTIONSTRING', 'REGEX' => 'REGEX', 'EMAIL' => { 'GENERAL' => 'GENERAL', 'CRITICAL' => 'CRITICAL', 'SMTPSERVER' => 'SMTPSERVER' }, 'DIR' => ['DIR1', 'DIR2'] };
becomes this:
$VAR1 = { 'incoming' => { 'dir' => 'DIR' }, 'frequency' => 'FREQUENCY', 'regex' => 'REGEX', 'rejected' => 'REJECTED', 'dir' => [ 'DIR1', 'DIR2' ], 'connectionstring' => 'CONNECTIONSTRING', 'email' => { 'general' => 'GENERAL', 'critical' => 'CRITICAL', 'smtpserver' => 'SMTPSERVER' } };
Update: Good eye, blakem, I never had occasion to run my hash through twice, but the added check fixes that problem.
sub force_uc_hash { my $hashref = shift; foreach my $key (keys %{$hashref} ) { $hashref->{uc($key)} = $hashref->{$key}; force_uc_hash($hashref->{uc($key)}) if ref $hashref->{$key} eq + 'HASH'; delete($hashref->{$key}) unless $key eq uc($key); } } sub force_lc_hash { my $hashref = shift; foreach my $key (keys %{$hashref} ) { $hashref->{lc($key)} = $hashref->{$key}; force_lc_hash($hashref->{lc($key)}) if ref $hashref->{$key} eq + 'HASH'; delete($hashref->{$key}) unless $key eq lc($key);; } }

In reply to Force nested hashes to upper/lower case keys by ryddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.