Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I convert entire hash to lowercase?

by CharlesClarkson (Curate)
on Nov 18, 2001 at 11:27 UTC ( #126101=perlquestion: print w/ replies, xml ) Need Help??
CharlesClarkson has asked for the wisdom of the Perl Monks concerning the following question:

I am attepting to pass an optional anonymous hash as the first argument to a function. I allow any case for the keys and values. I then convert the hash in the hash reference to all lowercase. I have isolated the method I am using to this small script. My question is: Is there a way to convert the keys and values of a hash to all lowercase without using a temporary hash? (I realize this will change the original hash.)

#!/usr/bin/perl use strict; use diagnostics; use Data::Dumper; my $hash_ref = { Extension => '.txt', Content => 'text', Directory => 'c:/' }; print Dumper $hash_ref; my %lc_hash; $lc_hash{lc $_[0]} = lc $_[1] while @_ = each %$hash_ref; $hash_ref = \%lc_hash; print Dumper $hash_ref; __END__



Thank You,
Charles K. Clarkson

Comment on How do I convert entire hash to lowercase?
Download Code
Re: How do I convert entire hash to lowercase?
by japhy (Canon) on Nov 18, 2001 at 11:50 UTC
    (Updated.) I'd do this:
    @hash{map lc, @k} = map lc, delete @hash{@k = keys %hash};
    Nifty?

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      I don't think I completely understand how exactly this piece of code works.
      Can someone explain ?

      thanks.
        I think I can...
        # @hash{map lc, @k} = # map lc, delete @hash{@k = keys %hash}; @keys = keys %hash; @lc_values = map { lc $_ } @hash{@keys}; # hash slice @lc_keys = map { lc $_ } @keys; delete @hash{@keys}; # basically: %hash = () @hash{@lc_keys} = @lc_values;
        </code>

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: How do I convert entire hash to lowercase?
by Zaxo (Archbishop) on Nov 18, 2001 at 12:40 UTC

    Sure, you have a reference; you can modify it. I don't know if this is exactly what you want. It doesn't allow for keys which differ only by case having different values:

    sub DeAOL { for ( keys %{$_[0]} ) { my $tmp = lc $_[0]->{$_}; delete $_[0]->{$_}; $_[0]->{lc $_} = $tmp; } }

    After Compline,
    Zaxo

Re: How do I convert entire hash to lowercase?
by chipmunk (Parson) on Nov 18, 2001 at 22:01 UTC
    This is similar to japhy's solution, but a little more straightforward: %hash = map lc, %hash;
      Very nice! Let's make it work on a hash reference:

      $h_ref = { map lc, %$h_ref };

      For those of you who don't quite follow, here are the rules:

      • In list context, a hash returns a list of key => value pairs. This is the same thing you use when you *create* a hash. There's no guaranteed order, though, which is rather the point.
      • map iterates over the elements of a list, applies some transformation to each, and produces an equally-sized list of the results.
      • map lc, %list iterates over the keys and values of the hash (by design, a value follows its key) and lowercases each.
      • The right side of the expression is evaluated first, so the same variable can be used on both sides.
      • Wrapping the map statement in curly braces turns the resulting list into an anonymous hash.</li.
      • Assigning that to the original hash reference makes sure the old, potentially upper-cased keys and values are no longer there.

      You can get by without the anonymous hash construction, but I thought it was more clear. map is nice.

      Oh, duh.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://126101]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-12-23 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (135 votes), past polls