Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How do I convert entire hash to lowercase?

by chipmunk (Parson)
on Nov 18, 2001 at 22:01 UTC ( #126138=note: print w/replies, xml ) Need Help??


in reply to How do I convert entire hash to lowercase?

This is similar to japhy's solution, but a little more straightforward: %hash = map lc, %hash;

Replies are listed 'Best First'.
Re: Re: How do I convert entire hash to lowercase?
by chromatic (Archbishop) on Nov 19, 2001 at 00:57 UTC
    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.

Re: Re: How do I convert entire hash to lowercase?
by japhy (Canon) on Nov 19, 2001 at 01:00 UTC
    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: note [id://126138]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2020-01-25 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?