Reversing a multi-value hash in perl -


I have a hash that looks like this:

  {bmw = & gt; ; {ID = & gt; 1, color = & gt; Brown}} {Mercedes = & gt; {ID = & gt; 2, color = & gt; Black}}  

I want to convert this hash to Pearl so that I get a map of ID => name_of_car. Do I need to use a reverse function somehow?

Note: I always reinterpret through the original hash and I can allocate keys and values ​​according to the new HSV, so I want to know what was a fun way.

<

  My% h = (BMW => {id =================== ================================================== <& Gt; 1, color = & gt; "brown"}, Mercedes => {id => 2, color = & gt; "black"}); My% j = map {($ h {$ _} {ID} = & gt; $ _}} for key% h; (keys% j) {print "$ _ $ j {$ _} \ n"; }  

Output:

 $ ./silly.pl 1 BMW 2 Mercedes 

Comments