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...

%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"; }
Discipulus edited some tag and html formatting

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

    The easiest way to convert array to hash,

    use strict; use warnings; use Data::Dumper; my @list= ("Andy", "1995", "Sarah", "1990", "Sam", "1992"); my %people = @list; print Dumper \%people;
    Output:
    $VAR1 = {
              'Andy' => '1995',
              'Sarah' => '1990',
              'Sam' => '1992'
            };
    
    

    All is well. I learn by answering your questions...
Re: How to change an Array to a Hash (updated)
by Discipulus (Canon) on Aug 01, 2018 at 07:15 UTC
    Hello alialialia,

    As already said you can feed a list to form an hash directly. But note venoth.ree added use warnings (and use strict too: never miss them) infact if your list is odd you can trap some unwanted behaviour:

    perl -we "my %hash = @ARGV" Andy 1995 Sarah 1990 Sam 1992 UNEXPECTED Odd number of elements in hash assignment at -e line 1.

    If you want to build the hash using a loop (ignoring odd element if present), you can use while as in:

    @ARGV = qw( Andy 1995 Sarah 1990 Sam 1992 UNEXPECTED ); my %hash; while (my $key = shift @ARGV and my $val = shift @ARGV){ $hash{$key}=$val } # hash is: ("Sam", 1992, "Andy", 1995, "Sarah", 1990)

    UPDATE you can spot the odd element before entering the loop: print qq(odd element $ARGV[-1] will be lost!\n) if @ARGV % 2 > 0; though.

    If you want you can also explore List::Util pairs function: it warns on odd element assigning undef

    use strict; use warnings; use List::Util qw(pairs); @ARGV= qw(Andy 1995 Sarah 1990 Sam 1992 UN); my %hash; for my $pairs (pairs @ARGV){ $hash{ $$pairs[0] } = $$pairs[1]; } Odd number of elements in pairs at -e line 1. # hash is: ("Sarah", 1990, "Andy", 1995, "UN", undef, "Sam", 1992)
    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How to change an Array to a Hash
by Laurent_R (Canon) on Aug 01, 2018 at 07:19 UTC
    Hi alialialia,

    vinoth.ree has provided an easy and good solution.

    But it might be even simpler to assign directly the list to the hash:

    my %people = ("Andy", "1995", "Sarah", "1990", "Sam", "1992");