alialialia has asked for the wisdom of the Perl Monks concerning the following question:
Whats the easiest way for me to change this array of alternating keys and values to a hash using a loop? In other words, I want Andy, Sarah, Sam to be the keys, and 1995, 1990, 1992 to be the corresponding values. And then organize it based on the values (highest first). I couldn't figure out the loop to sort them but I tried to write a code to organize them...
Discipulus edited some tag and html formatting%people = (); @list= ("Andy", "1995", "Sarah", "1990", "Sam", "1992"); $list[0]=keys %people; $list[1]=values people; print "sorting keys by value:\n"; foreach $name (sort {$people{$b}<=>$people{$a} }keys %people){ print "brand: $name, points: $people{$name}\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to change an Array to a Hash
by vinoth.ree (Monsignor) on Aug 01, 2018 at 06:01 UTC | |
|
Re: How to change an Array to a Hash (updated)
by Discipulus (Canon) on Aug 01, 2018 at 07:15 UTC | |
|
Re: How to change an Array to a Hash
by Laurent_R (Canon) on Aug 01, 2018 at 07:19 UTC |