savio has asked for the wisdom of the Perl Monks concerning the following question:

How can I convert a list to a hash data structure?

The output I expect it is an hash having the array's index as key and the array's value as value.

Thanks in advance for help

Replies are listed 'Best First'.
Re: converting an array to an hash
by choroba (Cardinal) on Dec 22, 2014 at 17:19 UTC
    In recent Perls, you can use the new slice syntax to convert an array to a hash:
    use Syntax::Construct qw{ %slice }; use Data::Dumper; my @l = qw( the quick brown fox ); my %h = %l[0 .. $#l]; print Dumper \%h; __END__ $VAR1 = { '2' => 'brown', '1' => 'quick', '3' => 'fox', '0' => 'the' };
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: converting an array to an hash
by toolic (Bishop) on Dec 22, 2014 at 17:11 UTC
    use warnings; use strict; my @arr = 'a' .. 'e'; my %hash; for my $i (0 .. $#arr) { $hash{$i} = $arr[$i]; } use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\%hash); __END__ $VAR1 = { '0' => 'a', '1' => 'b', '2' => 'c', '3' => 'd', '4' => 'e' };
Re: converting an array to an hash
by BrowserUk (Patriarch) on Dec 22, 2014 at 17:15 UTC
    How can I convert a list to a hash data structure?/i>

    Besies that you say "list" in one place and "array" in another which may indicate some confusion on your part (or mine), that sounds like a strange (if not dubious) think to want to do.

    But, if your sure it is what you need, here are two ways. Judge the comparative merits for yourself:

    sub listToIndexedHashRef{ my $n=0; my %hash = map{ $n++, $_} @_; return \%hash };; my $hashRef = listToIndexedHash( qw[the quick brown fox ] );; pp $hashRef;; { "0" => "the", 1 => "quick", 2 => "brown", 3 => "fox" } sub listToIndexedList{ my $n=0; return map{ $n++, $_} @_; };; my %hash = listToIndexedList( qw[the quick brown fox ] );; pp \%hash;; { "0" => "the", 1 => "quick", 2 => "brown", 3 => "fox" }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Indeed I could use just the following line:

      %hash = map { get_a_key_for($_) => $_ } @array;

      but I want something like this:
      %hash = (1 => value's list, 2 => values's list, n => value's list)

      How can I build that?
        but I want something like this:
        %hash = (1 => value's list, 2 => values's list, n => value's list)
        How can I build that?

        Sorry, but I have no idea what you mean by that?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: converting an array to an hash
by LanX (Saint) on Dec 22, 2014 at 22:35 UTC
    Nobody offered a hash slice yet?

    my @arr = 'a' .. 'e'; my %hash; @hash{0 .. $#arr} = @arr;

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)